Could you help me, please.
I have a class:
public class Product
{
...
// NOT REQUIRED!
public virtual Category Category{ get; set; }
}
But when in a view I create
@Html.HiddenFor(model => model.Category.Id), or
@Html.Hidden("model.Category.Id", model => model.Category.Id)
razor adds validation attribute to this.
I found out that this is not a razor problem, it is somewhere in MVC. Even if I manage to pass "Category.Id" value = "" to the server, TryModelUpdate() will fail - it requires "Category.Id" to be set, but it's not required in my model.
Why is it so??!
I solved the same issue with an crutch like this:
@{ Html.EnableUnobtrusiveJavaScript(false); }
@Html.HiddenFor(t => t.Prop1)
@Html.HiddenFor(t => t.Prop2)
...
@{ Html.EnableUnobtrusiveJavaScript(true); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With