質問

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