0

I have a View that has the ability to both create and edit an item that I have separated into partial views:

MainEditView.cshtml
_CreateChildDialog.cshtml
_EditChildDialog.cshtml

I have separate ViewModels for both the Create and Child items:

public class CreateChildViewModel
{
    public string ItemText { get; set; }
}


public class EditChildViewModel 
{
    public string ItemText { get; set; }
}

Since the partial views for the Edit and Create dialog boxes will both be rendered on the same page, I will have a conflict for form id's and names...since they are both called ItemText.

Is it possible to customize the binding of these elements without writing a custom model binder?

I would like to do something like:

public class EditChildViewModel
{
    [BindFrom("EditItemText")]
    public string ItemText { get; set; }
}

Or does it just make more sense to rename the ViewModel properties to:

public class EditChildViewModel 
{
    public string EditItemText { get; set; }
}

public class CreateChildViewModel
{
    public string CreateItemText { get; set; }
}

EDIT Based on converstation with Darin I want to make this a little more clear.

My Parent has an Edit action. When you edit the Parent, you would never create a new child or edit a child when you are calling the ParentController.Edit action.

I have a separate controller for the Child object that has a Create and Edit method:

public class ChildController 
{
    public ActionResult Edit() {}
    public ActionResult Create() {}
}

I am using jQuery calls to asynchronously post to this controller when you edit or create a child. Basically I use a jquery dialog to create/edit a child that will get saved immediately when I click Ok on the dialog. This would happen even before clicking save for the Edit action of the parent.

1 Answer 1

3

I would use editor templates. Normally you would pack those two view models into a main view model which will be used by the main view:

public class MyViewModel
{
    public CreateChildViewModel Create { get; set; }
    public EditChildViewModel Edit { get; set; }
}

and then:

@model MyViewModel
@Html.EditorFor(x => x.Create)
@Html.EditorFor(x => x.Edit)

and I would replace the two partials by their corresponding editor templates (~/Views/Shared/EditorTemplates/CreateChildViewModel.cshtml and ~/Views/Shared/EditorTemplates/EditChildViewModel.cshtml). The editor templates will take of generating proper names and ids of the corresponding input elements.

Personally I tend to prefer editor/display templates instead of partials as they handle better naming of input elements.

Sign up to request clarification or add additional context in comments.

3 Comments

Does this still apply if the CreateChild / EditChild calls are being made separate from the parent? I'm using a jquery AJAX call to submit it to a different controller than the "parent" item.
@Dismissile, so the actions you are posting to take respectively CreateChildViewModel and EditChildViewModel as arguments? In this case you could specify the prefix so that the default model binder can successfully bind it: public ActionResult Create([Bind(Prefix="Create")] CreateChildViewModel model) { ... }.
Yes. In my case, you would never save a Parent along with a child. I will update my post to more clearly reflect this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.