model-binding

post multi-select list to list of objects asp.net core 2

冷暖自知 提交于 2019-12-21 22:44:52
问题 I have two classes called User and Role. User is like this class User{ ... public List<Role> Roles; } and Role is like this class Role{ ... public int ID; public string Name; } now I have an action (Submit) public IActionResult Submit(User user){ ... } in my index.cshtml select is <select multiple="multiple" class="multi-select" id="my_multi_select1" asp-for="Roles" asp-items="ViewBag.Roles"></select> and ViewBag.Roles value is ViewBag.Roles= new SelectList(Role.SelectAll(), "Name", "Name");

How to handle MVC model binding prefix with same form repeated for each row of a collection?

陌路散爱 提交于 2019-12-21 20:23:41
问题 My main view model has a collection of ChildViewModel. In the view, I loop over the collection and call EditorFor(), like so: @for (int i = 0; i < Model.Children.Count; i++) { @Html.EditorFor(m => m.Child[i]); } The editor template looks like: @model ChildModel @using (Html.BeginForm("EditChild", "MyController")) { @Html.HiddenFor(m => m.ChildId) @Html.TextBoxFor(m => m.ChildName) } This will generate markup where each children is in a separate form, and each such form will have an input

MVC/Razor model binding collections when an element is missing

谁都会走 提交于 2019-12-21 12:17:34
问题 I've got a form that contains a variable length list of textboxes, rendered using a template similar to this.. @Html.TextBox("items[" + itemIndex + "].Title", someValue) So the final rendered HTML looks something like this... <input id="items_0__Amount" type="text" value="Apple" name="items[0].Title"> <input id="items_1__Amount" type="text" value="Banana" name="items[1].Title"> <input id="items_2__Amount" type="text" value="Orange" name="items[2].Title"> On form submission this binds to my

Modelbinding IEnumerable in ASP.NET MVC POST?

╄→尐↘猪︶ㄣ 提交于 2019-12-21 09:09:10
问题 Is there any issues with modelbinding IEnumerable types to an MVC POST? Some properties in my Model are not being bound upon a post to an action. Seems that properties on the model like strings are ok, but my IEnumerable is what is not being bound. Here's a snippet of my code: <%: Html.TextBoxFor(m => m.ResponseInfo.SubsetInfo.Test) %> <% for (int i = 0; i < Model.ResponseInfo.SubsetInfo.BandAvailabilities.Count(); i++) {%> <%: Html.TextBoxFor(m => m.ResponseInfo.SubsetInfo.BandAvailabilities

when passing a collection to EditorFor(), it generates invalid names for input elements

房东的猫 提交于 2019-12-20 14:19:20
问题 I have a BookCreateModel which consists of book's plane info such as Title, PublishYear & etc plus a collection of book Authors (complex type) : public class BookCreateModel { public string Title { get; set; } public int Year { get; set; } public IList<AuthorEntryModel> Authors { get; set; } } public class AuthorEntryModel { public string FirstName { get; set; } public string LastName { get; set; } } in CreateBook view I have used EditorFor helper : @Html.EditorFor(m => m.Authors,

Dynamic form and data binding with Spring MVC

北慕城南 提交于 2019-12-20 12:27:51
问题 In my Spring MVC application I need to implement a dynamic questionnaire form: I have N questions and for each I have 3 options. So in my page I'll have something like this: | Question 1 | 1 | 2 | 3 | | Question 2 | 1 | 2 | 3 | | Question 3 | 1 | 2 | 3 | | ... | 1 | 2 | 3 | | Question N | 1 | 2 | 3 | Questions are stored in a database and for the options I'll use radio buttons. I'll use a forEach tag to creare dynamic rows, but I don't know how to post data and handle ModelAttribute binding

ASP.NET MVC: How to 'model bind' a non html-helper element

落花浮王杯 提交于 2019-12-20 10:43:46
问题 Could you tell me a way(s) that I can bind a model property to a html-element, created without using html helper? In other words to a plain html element such as: <input type="text" /> 回答1: If you are referring to Model Binding, it does not require helpers, but naming convention. Helpers just make it easy and concise to create the HTML markup. You could create plain HTML inputs and just set the name attribute correctly. The default naming convention is just dot based, omitting the parent level

Asp.net core model doesn't bind from form

不问归期 提交于 2019-12-20 10:33:42
问题 I catch post request from 3rd-side static page (generated by Adobe Muse) and handle it with MVC action. <form method="post" enctype="multipart/form-data"> <input type="text" name="Name"> ... </form> Routing for empty form action: app.UseMvc(routes => routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}")); But in according action I have model with every property is empty Action: [HttpPost] public void Index(EmailModel email) { Debug.WriteLine("Sending email"); } Model

ASP.Net Web API custom model binding with x-www-form-urlencoded posted data - nothing seems to work

可紊 提交于 2019-12-20 09:59:37
问题 I am having a lot of trouble getting custom model binding to work when posting x-www-form-urlencoded data. I've tried every way I can think of and nothing seems to produce the desired result. Note when posting JSON data, my JsonConverters and so forth all work just fine. It's when I post as x-www-form-urlencoded that the system can't seem to figure out how to bind my model. My test case is that I'd like to bind a TimeZoneInfo object as part of my model. Here's my model binder: public class

MVC3 ModelBinding to a collection posted back with index gaps

好久不见. 提交于 2019-12-19 08:27:49
问题 I have a collection of objects on my Model that I'm rendering in a View by using EditFor function, and I have an EditorTemplate which is responsible for actually rendering each object. @Html.EditorFor(model => model.MyObjects) This has worked well for a while now, and when you check the html, my text boxes are prefixed with the model property, followed by the index of the collection they came from. <input class="text-box single-line" id="MyObjects_2__SomeProperty" name="MyObjects[2]