J
J
jammywork12015-03-18 16:15:57
JavaScript
jammywork1, 2015-03-18 16:15:57

How to build the structure of html input' s so that the Action accepts a serialized object?

example:

ObjectModel
public int id;
public string password;
public bool tired;

View receives IEnumerable
How to display them so that when submitting the form in Action I get objects back now I get them like this
public void Action(ObjectModel[] objects)
public void Action(int[] ids, string[] passwords, bool[] tireds)

Certainly not convenient, and not beautiful.
I draw the conclusion using HTML Helpers
@foreach (var obj in Model) {
    @Html.TextBoxFor(m => obj.Id) <br />
    @Html.TextBoxFor(m => obj.password) <br />
    @Html.CheckBoxFor(m => obj.tired) <br />
    <br />
    <br />
}


that's what happens

<input data-val="true" data-val-number="Значением поля Id должно быть число." data-val-required="Требуется поле Id." id="obj_Id" name="obj.Id" type="text" value="109" /> <br />
<input id="obj_password" name="obj.password" type="text" value="reader" /> <br />
<input data-val="true" data-val-required="Требуется поле tired." id="obj_tired" name="obj.tired" type="checkbox" value="true" />
<input name="obj.tired" type="hidden" value="false" /><br /><br /><br />
<input id="obj_Id" name="obj.Id" type="text" value="108" /> <br />
<input id="obj_password" name="obj.password" type="text" value="reguser" /> <br />
<input id="obj_tired" name="obj.tired" type="checkbox" value="true" /><input name="obj.tired" type="hidden" value="false" /> <br /><br /><br />
<input id="obj_Id" name="obj.Id" type="text" value="4" /> <br />
<input id="obj_password" name="obj.password" type="text" value="Зарегистрированный пользователь" /> <br />
<input id="obj_tired" name="obj.tired" type="checkbox" value="true" /><input name="obj.tired" type="hidden" value="false" /> <br /><br /><br />

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Grechushnikov, 2015-03-18
@maxyc_webber

offtopic. oh my god. How terrible does this code look?

G
Grigory Vashkevich, 2015-03-19
@konar

Purely theoretically, you need to form inputs as follows:

<div>
<input  name="[0]obj.Id" type="text"  /> <br />
<input  name="[0]obj.password" type="text"  /> <br />
<input name="[0]obj.tired"  type="text"/><br />
</div>
<div>
<input  name="[1]obj.Id" type="text"  /> <br />
<input  name="[1]obj.password" type="text"  /> <br />
<input name="[1]obj.tired" type="text" /><br />
</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question