views:

30

answers:

2

I have the following ViewData that I pass into a view.

public class MerchantSignUpViewData : BaseViewData
{
    public Merchant Merchant { get; set; }
    public Address Address { get; set; }
    public Deal Deal { get; set; }
    public List<MerchantContact> Contacts { get; set; }
    public int TabIndex { get; set; }
    public List<DealPricing> DealPricing { get; set; }

}

I also created 3 partial views. Merchant Info, Address, Merchant Properties

In my View I have a Deal Model that shares the same field names as Merchant which is "Name"

I can't put these in the same form cause the names will be the same.

What I ended up doing was putting all 10 partial views into one huge form (I started crying at this point) and bound like this.

<%: Html.TextBoxFor(model => model.Deal.Name)%>
<%: Html.TextBoxFor(model => model.Deal.Name)%>

This gives me the correct names of the form elements.

What I want to do is the following.

<% Html.RenderPartial("MerchantForm", Model.Merchant) %>
<% Html.RenderPartial("DealForm", Model.Deal) %>

But how do I add a prefix to all the TextBoxFor pieces or preferable the render partial tags.

Hope I provided enough information or maybe I'm just doing this the wrong way. Either will help me in the long run so thanks in advance.

A: 

(I started crying at this point)

I would recommend you not do mvc the way you are doing at this moment because you are always going to have a lot of problems.

you are passing entities to a view (which is a form), that is a bad practice (at least for complex forms, if you would had Foo{fname,lname} it would be ok)

I'm not really sure what you want to achieve but my recommendation for you is to use a ViewModel that is going to represent exactly the form that you want your user to submit

Omu
So you are saying that even though I have an address form all over the system in different views I should recreate the form in all my views? I'm fairly new to MVC but there seems like we could create something a little more reusable?
MIchael Grassman
@MIchael Grassman you can create a resusable AddressViewModel, the point is that html doesn't work with entities, it works with everything what the modelbinder does: int, string, IEnumerable<SelectListItem>, bool
Omu
+1  A: 

Maybe I'm not quite getting the problem but I think this is exactly what Html.EditorFor(x=>x...) is for.

Create a folder called "EditorTemplates" in the same directory where your views are. Put your partials in here and give them the same name as your model type (eg rename "MerchantForm.ascx" to "Merchant.ascx").

In your main view instead of

Html.RenderPartial("MerchantForm", Model.Merchant)

use

Html.EditorFor(x=>x.Merchant)

The templating system will deal with the prefixes for you so that on post the model binder will tie everything up properly.

If you have templates set up for all the complex objects in the model you can even go a step further and on your main view just call

Html.EditorForModel()

Which will reflect over the properties in your model and call their relevant editors.

Chao
I'll have to look into this. I'm always afraid to you anything that contains the word template as my clients don't understand that phrase at all. Thanks
MIchael Grassman
In terms of ASP MVC a simple way of describing it is basically a partial that handles the prefix for you.
Chao