문제

I have the following piece of code :

@if (Model.A == Model.B)
{
    Html.Hidden("a1", Model.A1);
}
else
{
    Html.Hidden("a2", Model.A2);
}

With the above piece of code the hidden fields are not created and I don't get any errors.

After 30 minutes I realized that if I put the @ behind Html.Hidden it works :

@if (Model.A == Model.B)
{
    @Html.Hidden("a1", Model.A1);
}
else
{
    @Html.Hidden("a2", Model.A2);
}

Any ideas ?

Thanks

도움이 되었습니까?

해결책

Html is a form property that maps to an instance of HtmlHelper. HtmlHelper.Hidden returns a MvcHtmlString

Html.Hidden("a1", Model.A1);

does nothing as the returned value is not captured. You don't get any errors because it's perfectly valid C# code (capturing the return is optional). Most static analysis tools, however, will warn you about this since it's most likely a bug (as you have discovered).

On the other hand,

@Html.Hidden("a1", Model.A1);

is analagous to

Response.Write(Html.Hidden("a1", Model.A1));

which writes the return value to the HTML response.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top