0

I want to add dynamicly new partial form on page. I used this code:

 Guid index = Guid.NewGuid();

<input type="hidden" name="People.index" value="@index" />
<hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.surname, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBoxFor(model => model.surname, new { name = "Author[index].surname", htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.surname, "", new { @class = "text-danger" })
        </div>
    </div>

But in source code after page loading i see this:

 <div class="form-group">
        <label class="control-label col-md-2" for="surname">Фамилия</label>
        <div class="col-md-10">
            <input htmlAttributes="{ class = form-control }" id="surname" name="surname" type="text" value="" />
            <span class="field-validation-valid text-danger" data-valmsg-for="surname" data-valmsg-replace="true"></span>
        </div>
    </div>

How i can enabled Guid in name field? And how change fields in TextBoxFor?

Vasiliy Terkin
  • 165
  • 2
  • 15
  • 2
    you can change the name by using proper case `Name` in your htmlAttributes object. `@Html.TextBoxFor(model => model.surname, new { Name = "Author[index].surname"})` using Guid in the name is not really going to befenit you in any way i'm afraid – JamieD77 Sep 01 '15 at 20:55
  • @JamieD77, Yes it will because OP is including a hidden input with `name="People.index" value="@index"` except that it wont work because it needs to be `name="Index`. But this is not the correct approach anyway. –  Sep 01 '15 at 22:55
  • Look at using the `BeginCollectionItem` helper or if you want a pure client side approach refer the answers [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) and [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308). Note also your hidden input needs `name="Index"` (not `name="People.index")` and your `ValidationMessageFor()` wont work since it will not match a model property. –  Sep 01 '15 at 22:59
  • Try TextBox insteadof TextBoxFor, I think easily you can give the name you want – Nick Mehrdad Babaki Sep 02 '15 at 03:04

0 Answers0