@Html.HiddenFor(model => model.title, new { id= "natureOfVisitField", @value = '@Model.title'})
it doesen't work! how to set the value?
@Html.HiddenFor(model => model.title, new { id= "natureOfVisitField", @value = '@Model.title'})
it doesen't work! how to set the value?
You shouldn't need to set the value in the attributes parameter. MVC should automatically bind it for you.
@Html.HiddenFor(model => model.title, new { id= "natureOfVisitField" })
[HttpGet]
, or something like that. Unfortunately I don't have time to go back right now and recreate it and nail it down. –
Althing For setting value in hidden field do in the following way:
@Html.HiddenFor(model => model.title,
new { id= "natureOfVisitField", Value = @Model.title})
It will work
Value
and value
tags. You want to use value
because that's where the controller will look for your data –
Checkerberry Strange but, Try with @Value
, capital "V"
e.g. (working on MVC4)
@Html.HiddenFor(m => m.Id, new { @Value = Model.Id })
Update:
Found that @Value (capital V) is creating another attribute with "Value" along with "value", using small @value seems to be working too!
Need to check the MVC source code to find more.
Update, After going through how it works internally:
First of all forget all these workarounds (I have kept in for the sake of continuity here), now looks silly :)
Basically, it happens when a model is posted and the model is returned back to same page.
The value is accessed (and formed into html) in InputHelper method (InputExtensions.cs) using following code fragment
string attemptedValue = (string)htmlHelper.GetModelStateValue(fullName, typeof(string));
The GetModelStateValue method (in Htmlelper.cs) retrieves the value as
ViewData.ModelState.TryGetValue(key, out modelState)
Here is the issue, since the value is accessed from ViewData.ModelState
dictionary.
This returns the value posted from the page instead of modified value!!
i.e. If your posted value of the variable (e.g. Person.Id) is 0 but you set the value inside httpPost action (e.g. Person.Id = 2), the ModelState still retains the old value "0" and the attemptedValue contains "0" ! so the field in rendered page will contain "0" as value!!
Workaround if you are returning model to same page : Clear the item from ModelState,
e.g.
ModelState.Remove("Id");
This will remove the item from dictionary and the ViewData.ModelState.TryGetValue(key, out modelState) returns null, and the next statement (inside InputExtensions.cs) takes the actual value (valueParameter) passed to HiddenFor(m => m.Id)
this is done in the following line in InputExtensions.cs
tagBuilder.MergeAttribute("value", attemptedValue ?? ((useViewData) ? htmlHelper.EvalString(fullName, format) : valueParameter), isExplicitValue);
Summary:
Clear the item in ModelState using:
ModelState.Remove("...");
Hope this is helpful.
ModelState.Remove("Invoice.InvoiceId");
–
Allista ModelState.Clear();
instead of explicitly deleting the ID. Of course only if the ModelState does not contain any errors. I call clear when ModelState.IsValid == true
and the data has been successfully saved. –
Carreon ModelState
not from actual Model
. It's a long time gotcha that trips up a lot of people. A good explanation why it works that way is this blog weblog.west-wind.com/posts/2012/apr/20/… Arguably it makes less sense for "Hidden" helper, but I guess at least it's consistent with other helpers. As mentioned you can just use model explicitly to render hidden input, like <input id="Id" name="Id" type="hidden" value="@Model.Id" />
instead of clearing model state. –
Cardoza You shouldn't need to set the value in the attributes parameter. MVC should automatically bind it for you.
@Html.HiddenFor(model => model.title, new { id= "natureOfVisitField" })
[HttpGet]
, or something like that. Unfortunately I don't have time to go back right now and recreate it and nail it down. –
Althing It is just Value, not @value.. Try it. I'm not sure about @Model.title, maybe it's just Model.title
Value
–
Salinometer Necroing this question because I recently ran into the problem myself, when trying to add a related property to an existing entity. I just ended up making a nice extension method:
public static MvcHtmlString HiddenFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, TProperty value)
{
string expressionText = ExpressionHelper.GetExpressionText(expression);
string propertyName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expressionText);
return htmlHelper.Hidden(propertyName, value);
}
Use like so:
@Html.HiddenFor(m => m.RELATED_ID, Related.Id)
Note that this has a similar signature to the built-in HiddenFor, but uses generic typing, so if Value is of type System.Object, you'll actually be invoking the one built into the framework. Not sure why you'd be editing a property of type System.Object in your views though...
The @ symbol when specifying HtmlAttributes is used when the "thing" you are trying to set is a keyword c#. So for instance the word class, you cannot specify class, you must use @class.
You can do this way
@Html.HiddenFor(model=>model.title, new {ng_init = string.Format("model.title='{0}'", Model.title) })
© 2022 - 2024 — McMap. All rights reserved.