Notice your are passing an expression tree to the helperfor, not the model value. The helperfor converts the expression tree to a string, in your case the string “id” and calls the non-for version of the helper.
When a helper is looking up the value of “id” it has several possible locations. It can be in route values collection, it can be a property or variable in the view, form values collection, or it can a model property. The helpers alway looks in the route collection
and forms collection before the model.
So if the model has a different value you want to use the common practice clear this collection.
ModelState.Clear();
note: after doing this only the model and view variables are accessible from the view.
Yes, for helperfor expression tree, you are right about it and also ModelState.Clear() works for it, but don't get the relation between ModelState and route values. Most of the documentations says that ModelStateDictionary only worries about post data from
view to controller where in my example I am only using get method. Could you please let me know the explanations.
** Another thing is that in get/post method(string id), id is the part of model bindings.
Member
4 Points
21 Posts
Why URL parameter Id and model member Id value become same
Jan 11, 2018 11:23 AM|aspnetlearning|LINK
Hi, Everyone.
I am getting such problem that, when i pass a model with Id parameter and browsing that view without URL Id parameter
@Html.HiddenFor(m=>m.Id) showing the model member Id value but when i browse that view with a URL Id parameter then
@Html.HiddenFor(m=>m.Id) showing the URL Id parameter value ignoring model member Id value. This scenario is not clear to me.
Please let me know the proper reason why its happening. Here is my example code as follows.
---------------------- View -------------------------------------
@using FAV.Models
@model TestViewModel
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit</h2>
@using (Html.BeginForm("Edit", "Test", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal form-label-left input_mask", enctype = "multipart/form-data" }))
{
@Html.HiddenFor(m=>m.Id)
}
---------------- View End ------------------------
---------------- Model -------------------------------
public class TestViewModel
{
public Int64 Id { get; set; }
public string Name { get; set; }
}
---------------- Model End ---------------------------
------------------- Controller ---------------------------
public ActionResult Edit (string id, string returnUrl)
{
TestViewModel model = new TestViewModel()
{
Id = 3,
Name = "Rahim"
};
return View(model);
}
------------------- Controller End -----------------------
URL are as follows
http://localhost:8106/Test/Edit
http://localhost:8106/Test/Edit/4
Software Developer
All-Star
48674 Points
11071 Posts
Re: Why URL parameter Id and model member Id value become same
Jan 11, 2018 03:17 PM|bruce (sqlwork.com)|LINK
Notice your are passing an expression tree to the helperfor, not the model value. The helperfor converts the expression tree to a string, in your case the string “id” and calls the non-for version of the helper.
When a helper is looking up the value of “id” it has several possible locations. It can be in route values collection, it can be a property or variable in the view, form values collection, or it can a model property. The helpers alway looks in the route collection and forms collection before the model.
So if the model has a different value you want to use the common practice clear this collection.
ModelState.Clear();
note: after doing this only the model and view variables are accessible from the view.
Member
4 Points
21 Posts
Re: Why URL parameter Id and model member Id value become same
Jan 11, 2018 07:12 PM|aspnetlearning|LINK
Yes, for helperfor expression tree, you are right about it and also ModelState.Clear() works for it, but don't get the relation between ModelState and route values. Most of the documentations says that ModelStateDictionary only worries about post data from view to controller where in my example I am only using get method. Could you please let me know the explanations.
** Another thing is that in get/post method(string id), id is the part of model bindings.
Software Developer