Created
September 10, 2014 19:12
-
-
Save xDaevax/bd35b5d88fed03709d30 to your computer and use it in GitHub Desktop.
Generic List ModelBinding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Diagnostics; | |
namespace AjaxMvcTest.Controllers { | |
public class HomeController : Controller { | |
// | |
// GET: /Home/ | |
public ActionResult Index() { | |
HomeModel model = new HomeModel(); | |
model.Header = "Default Header"; | |
model.Content = "People assume that time is a strict progression of cause to effect, but actually, from a non-linear, non-subjective viewpoint, it's more like a big ball of wibbly-wobbly... timey-wimey... stuff."; | |
return View(model); | |
} | |
[ValidateInput(false)] | |
public ActionResult SaveContent(UpdateDataModel updateRequest) { | |
//Do some stuff | |
HomeModel newModel = new HomeModel(); | |
newModel.Content = updateRequest.Content; | |
newModel.Header = updateRequest.Header; | |
Debug.Assert(updateRequest.RandomData != null && updateRequest.RandomData.Any()); // Yields our desired result. | |
Debug.Assert(updateRequest.CustomData != null && updateRequest.CustomData.Any()); //Yields an array with 1 index of comma separated values because the model binder was confused. | |
Debug.Assert(updateRequest.SpecialData != null); //Will build a generic List<CustomModel> but doesn't know how to fill it | |
return View("Index", newModel); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomModel { | |
public string Name { get; set; } | |
public int Id { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HomeModel { | |
public string Header { get; set; } | |
public string Content { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
public class UpdateDataModel { | |
public string Content { get; set; } | |
public string Header { get; set; } | |
public DateTime TimeStamp { get; set; } | |
public IList<String> RandomData { get; set; } | |
public IList<String> CustomData { get; set; } | |
public IList<CustomModel> SpecialData { get; set; } | |
public int Id { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model HomeModel | |
@{ | |
ViewBag.Title = "Index"; | |
Layout = "~/Views/Shared/_Layout.cshtml"; | |
} | |
<h2>Index2</h2> | |
<form method="post" action="home/SaveContent"> | |
Header: <input type="text" name="HeaderInput" id="HeaderInput" value="@Model.Header" /> | |
<br /> | |
Content: <textarea name="ContentInput" id="ContentInput">@Model.Content</textarea> | |
<br /> | |
@for (var i = 0; i < 9; i++) { | |
@Html.Hidden("RandomData", i) | |
} | |
@Html.Hidden("CustomData", "1,2,3,4,5,6,7") | |
@for (var i = 0; i < 3; i++) { | |
@Html.Hidden("SpecialData", i) //Will not work because the model binder requires more information to build a CustomModel than an int or string | |
} | |
<input type="submit" id="saveFileBtn" value="Save" /> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment