Validating multiple instances of same partial view

npinti

I have a scenario in which I would like to display data to users and allow them to be able to modify it. To do this, I have a form which is preset with the current object values. This form is enclosed as a partial view which is in turn called in a loop depending on how many child objects my parent object has.

I also provide an empty form at the end to allow users to create new instances of this child item, so in short I have something like so:

Edit Form (a partial view):

@model WebPortal.Models.AddressModel

@using (Html.BeginForm("EditAddress", "MerchantDetailsEditor", FormMethod.Post))
{
    @Html.ValidationSummary(true)

    @Html.LabelFor(model => model.StreetNumber, new { id = Model.ID + "_streetnumber_label", Name = Model.ID + "_streetnumber_label", @for = Model.ID + "_streetnumber" })
    @Html.TextBoxFor(address => address.StreetNumber, new { id = Model.ID + "_streetnumber", Name = Model.ID + "_streetnumber"})
    @Html.ValidationMessageFor(address => address.StreetNumber)

Create Form (another partial view)

@model WebPortal.Models.AddressModel
@{
    int counter = 1;
}

@using (Html.BeginForm("AddAddress", "MerchantDetailsEditor", FormMethod.Post))
{
    @Html.ValidationSummary(true)

    @Html.LabelFor(model => model.StreetNumber, new { id = counter + "_streetnumber_label", Name = counter + "_streetnumber_label", @for = counter + "_streetnumber" })
    @Html.TextBoxFor(address => address.StreetNumber, new { id = counter + "_streetnumber", Name = counter + "_streetnumber"})
    @Html.ValidationMessageFor(address => address.StreetNumber)
    counter++;

Which are in turn called like so:

@foreach (WebPortal.Models.AddressModel address in @Model.Addresses)
{
    @Html.Partial("Edit_Form", address)
}
@Html.Partial("Add_Form", new WebPortal.Models.AddressModel())

The problem I am facing is that since they are all sharing the same model, the validation errors are being applied and shown next to all instances I have displayed. So if I where to edit an object, I would get errors because the other form (the add form) was empty.

I have read that this is due to the fact that MVC applies the component ID's at a view level, so since I am using multiple instances of the same view, they are all getting the same ID's.

To go round this I overrode the properties for the ID and Name, but to no avail.

Essentially, I just want the error messages to be shown next to the appropriate form. I am fairly green on this type of approach, so if it is incorrect please do let me know. My objective is to allow users to View/Edit and Create data on a particular screen.

Thanks!

npinti

I eventually managed to get this working. After coming across this previous SO question, I opted to use Actions instead. So the code now looks something like so:

EDIT Form (Still a partial view)

@model WebPortal.Models.AddressModel


@using (Html.BeginForm("EditAddress", "MerchantDetailsEditor", FormMethod.Post))
{
    @Html.ValidationSummary(true)

    @Html.HiddenFor(model => model.ID)

    @Html.LabelFor(model => model.StreetNumber, new { id = Model.ID + "_streetnumber_label" })
    @Html.TextBoxFor(address => address.StreetNumber, new { id = Model.ID + "_streetnumber" })
    @Html.ValidationMessageFor(address => address.StreetNumber)
 ...

Create Form (Still a partial view)

@model WebPortal.Models.AddressModel
@{
    int counter = 1;
}

@using (Html.BeginForm("AddAddress", "MerchantDetailsEditor", FormMethod.Post))
{
    @Html.ValidationSummary(true)

    @Html.LabelFor(model => model.StreetNumber, new { id = counter + "_streetnumber_label" })
    @Html.TextBoxFor(address => address.StreetNumber, new { id = counter + "_streetnumber"})
    @Html.ValidationMessageFor(address => address.StreetNumber)
 ...

The major change is what I did next:

<h2>Addresses</h2>
@foreach (WebPortal.Models.AddressModel address in @Model.Addresses)
{
    @Html.Action("_AddressEdit", address)
}

@if (ViewBag.AddressToAdd != null)
{
    @Html.Action("_AddressAdd", (WebPortal.Models.AddressModel)ViewBag.AddressToAdd)
}
else
{
    @Html.Action("_AddressAddNew")
}

I essentially provided an action per instance of my partial view. This allowed me to go round the problem exposed in the link in my answer.

My controller now looks like so:

 #region Actions
    public ActionResult _AddressEdit(AddressModel addressModel)
    {
        return View("...", addressModel);
    }

    public ActionResult _AddressAdd(AddressModel addressModel)
    {
        return View("...", addressModel);
    }

    public ActionResult _AddressAddNew()
    {
        return View("...");
    }
    #endregion Actions

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Validating multiple instances of same partial view

From Dev

Model binding doesn't work when multiple instances of the same partial view are called dynamically with ajax.actionlink in MVC 5

From Dev

Model binding doesn't work when multiple instances of the same partial view are called dynamically with ajax.actionlink in MVC 5

From Dev

Managing multiple instances of the same view controller

From Dev

Multiple instances of partial views and model binding on same page with MVC

From Dev

Rendering same partial view multiple times - Only the last partial view is rendered

From Dev

Validating multiple forms on the same page

From Dev

validating multiple inputs at the same time

From Dev

multiple instances, same ids

From Dev

Binding multiple Caliburn.Micro View Models to different instances of the same view

From Dev

CakePHP - Validating multiple fields with same rule

From Dev

Create and Index in same view using partial view

From Dev

multiple instances of view helper plugin?

From Dev

xcode - multiple instances of custom view

From Dev

Multiple instances of the same form field

From Dev

Multiple instances of the same service in Delphi

From Dev

populate multiple instances of the same #DIV

From Dev

Multiple instances of same react component

From Dev

launching multiple instances of same exe

From Dev

Using multiple instances of the same service

From Dev

tmux multiple instances of the same session

From Dev

Multiple Instances of the same project/program

From Dev

OnBackPressed multiple instances of same activity

From Dev

launching multiple instances of same exe

From Dev

Creating multiple instances of the same id

From Dev

Multiple instances of the same UIView in UIScrollView

From Dev

Multiple Hazelcast instances on same machine?

From Dev

Can one avoid multiple DOM elements with same id, when using Backbone/Marionette view instances?

From Dev

Loading a Partial View and changing the URL at the same time

Related Related

  1. 1

    Validating multiple instances of same partial view

  2. 2

    Model binding doesn't work when multiple instances of the same partial view are called dynamically with ajax.actionlink in MVC 5

  3. 3

    Model binding doesn't work when multiple instances of the same partial view are called dynamically with ajax.actionlink in MVC 5

  4. 4

    Managing multiple instances of the same view controller

  5. 5

    Multiple instances of partial views and model binding on same page with MVC

  6. 6

    Rendering same partial view multiple times - Only the last partial view is rendered

  7. 7

    Validating multiple forms on the same page

  8. 8

    validating multiple inputs at the same time

  9. 9

    multiple instances, same ids

  10. 10

    Binding multiple Caliburn.Micro View Models to different instances of the same view

  11. 11

    CakePHP - Validating multiple fields with same rule

  12. 12

    Create and Index in same view using partial view

  13. 13

    multiple instances of view helper plugin?

  14. 14

    xcode - multiple instances of custom view

  15. 15

    Multiple instances of the same form field

  16. 16

    Multiple instances of the same service in Delphi

  17. 17

    populate multiple instances of the same #DIV

  18. 18

    Multiple instances of same react component

  19. 19

    launching multiple instances of same exe

  20. 20

    Using multiple instances of the same service

  21. 21

    tmux multiple instances of the same session

  22. 22

    Multiple Instances of the same project/program

  23. 23

    OnBackPressed multiple instances of same activity

  24. 24

    launching multiple instances of same exe

  25. 25

    Creating multiple instances of the same id

  26. 26

    Multiple instances of the same UIView in UIScrollView

  27. 27

    Multiple Hazelcast instances on same machine?

  28. 28

    Can one avoid multiple DOM elements with same id, when using Backbone/Marionette view instances?

  29. 29

    Loading a Partial View and changing the URL at the same time

HotTag

Archive