Answered by:
How to ignore the previous bound value in model and take the newer one.

Question
-
User1052662409 posted
Hi All,
I have a list in the model and I am binding this to the page. (biding a disabled textbox with model property)
Again I am binding same list with another textbox and this time text box is editable.
// 1st time binding @for (int i = 0; i < Model.vendorsModels.Count; i++) { Html.TextBox("name", Model.vendorsModels[i].name, new { @name = "name", @class = "form-control" ,disabled= "disabled"}) } // 2nd time binding @for (int i = 0; i < Model.vendorsModels.Count; i++) { Html.TextBox("name", Model.vendorsModels[i].name, new { @name = "name", @class = "form-control" }) }
When I call mt ajax method to count the model property count it shows 2, obviously cos model has bouned two times.
What I want is that the ajax should ignore the first one bounded textbox (which is disabled) it should only count the editable model property )
Means it should count only 1 instead of 2 the latest one.
Thursday, April 30, 2020 10:47 AM
Answers
-
User-1330468790 posted
Hi demoninside9,
I would suggest you use a strongly typed extension method "TextBoxFor" to do the databind which will bind a specified model object property to input text so that it automatically displays a value of the model property in a textbox and visa-versa.
In your case, I think what you want to do is to post the editable vendor list data (second text box group) to the controller and render them in the first TextBox group.
You could refer to below demo where I don't change the first text box group and just modify the second text box group to the strongly typed method "TextBoxFor"
@model MVCCaseDemo.Models.VendorViewModel @{ ViewBag.Title = "VendorView"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>VendorView</h2> @using (Html.BeginForm("VendorView", "UserAccess", FormMethod.Post)) { // 1st time binding for (int i = 0; i < Model.vendorsModels.Count; i++) { @Html.TextBox("name", Model.vendorsModels[i].name, new { @name = "name", @class = "form-control", disabled = "disabled" }); } // 2nd time binding for (int i = 0; i < Model.vendorsModels.Count; i++) { @Html.TextBoxFor(m=>m.vendorsModels[i].name, new { @name = "name", @class = "form-control" }); } <input type="submit" value="Submit" /> }
Controller: Including the simulation of data
public static VendorViewModel model = new VendorViewModel() { vendorsModels = new List<Vendor> { new Vendor() { name = "name1" }, new Vendor() { name = "name2" }, new Vendor() { name = "name3" } } }; [HttpGet] public ActionResult VendorView() { return View("VendorView",model); } [HttpPost] public ActionResult VendorView(VendorViewModel viewModel) { model = viewModel; return View("VendorView", model); }
Demo:
Note that you should use "for" loop instead of using "foreach" to bind data for the text box since the model is a nested collection.
Hope this can help you.
Best regards,
Sean
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 1, 2020 7:55 AM
All replies
-
User475983607 posted
Add a class and use a standard class selector.
// 1st time binding @for (int i = 0; i < Model.vendorsModels.Count; i++) { Html.TextBox("name", Model.vendorsModels[i].name, new { @name = "name", @class = "form-control binding-1" ,disabled= "disabled"}) } // 2nd time binding @for (int i = 0; i < Model.vendorsModels.Count; i++) { Html.TextBox("name", Model.vendorsModels[i].name, new { @name = "name", @class = "form-control binding-2" }) }
$(.'binding-2').length
However seems like a poor design. Can you explain the problem you are trying to solve rather than the solution?
Thursday, April 30, 2020 11:16 AM -
User1052662409 posted
mgebhard
However seems like a poor design. Can you explain the problem you are trying to solve rather than the solution?Yes.
mgebhard
@for (int i = 0; i < Model.vendorsModels.Count; i++) { Html.TextBox("name", Model.vendorsModels[i].name, new { @name = "name", @class = "form-control binding-1" ,disabled= "disabled"}) }
The above is just for what the other person has done previously (Just to show here only no need to edit. But must have the data here to show.)
mgebhard
// 2nd time binding @for (int i = 0; i < Model.vendorsModels.Count; i++) { Html.TextBox("name", Model.vendorsModels[i].name, new { @name = "name", @class = "form-control binding-2" }) }
The above is binding the same data, but the user may add more names or may edit the previous name one and then finally submit to store the changes in the database.
Thanks
Thursday, April 30, 2020 11:27 AM -
User-1330468790 posted
Hi demoninside9,
I would suggest you use a strongly typed extension method "TextBoxFor" to do the databind which will bind a specified model object property to input text so that it automatically displays a value of the model property in a textbox and visa-versa.
In your case, I think what you want to do is to post the editable vendor list data (second text box group) to the controller and render them in the first TextBox group.
You could refer to below demo where I don't change the first text box group and just modify the second text box group to the strongly typed method "TextBoxFor"
@model MVCCaseDemo.Models.VendorViewModel @{ ViewBag.Title = "VendorView"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>VendorView</h2> @using (Html.BeginForm("VendorView", "UserAccess", FormMethod.Post)) { // 1st time binding for (int i = 0; i < Model.vendorsModels.Count; i++) { @Html.TextBox("name", Model.vendorsModels[i].name, new { @name = "name", @class = "form-control", disabled = "disabled" }); } // 2nd time binding for (int i = 0; i < Model.vendorsModels.Count; i++) { @Html.TextBoxFor(m=>m.vendorsModels[i].name, new { @name = "name", @class = "form-control" }); } <input type="submit" value="Submit" /> }
Controller: Including the simulation of data
public static VendorViewModel model = new VendorViewModel() { vendorsModels = new List<Vendor> { new Vendor() { name = "name1" }, new Vendor() { name = "name2" }, new Vendor() { name = "name3" } } }; [HttpGet] public ActionResult VendorView() { return View("VendorView",model); } [HttpPost] public ActionResult VendorView(VendorViewModel viewModel) { model = viewModel; return View("VendorView", model); }
Demo:
Note that you should use "for" loop instead of using "foreach" to bind data for the text box since the model is a nested collection.
Hope this can help you.
Best regards,
Sean
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 1, 2020 7:55 AM