سؤال

I have an MVC4 application in which I have built a view containing a form to edit a particular entity. Many of the fields in this form are bound to a strongly-typed model (Comments, Concerns, etc..), however a number of other fields are already set and are not being modified (Id, CreatedDate, etc..). In order to pass the values such as (Id, etc) back to the controller upon a form POST I have been using hidden fields as seen below..

<form id="myForm" method="post" action="/SaveMyModel">
  @model myModel

  @Html.TextAreaFor(i => i.Comments)
  @Html.TextAreaFor(i => i.Concerns)

  @Html.Hidden(i => i.Id)
  @Html.Hidden(i => i.CreatedDate)

  <input type="submit" value="Submit" />
</form>

It seems to me that there might be some 'syntactic sugar' which allows these hidden fields to be passed to the controller in bulk rather than using (in my current case) 10-15 hidden fields to pass each one explicitly.

Any suggestions?

هل كانت مفيدة؟

المحلول

I don't think there is anything built in. With that said, it would be pretty easy to make your own custom HTML Helper class that you can pass in all the required hidden fields as string or even the model properties and it will create all the hidden fields for you. I have found one tutorial on how to do this (please find it below) but I'm sure you can find many more just by googling custom html helper mvc tutorial or something like that. Hope this helps :)

http://dailydotnettips.com/2011/03/14/creating-custom-html-helpers-in-asp-net-mvc/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top