0

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.

Michael Grassman
  • 1,935
  • 13
  • 21
  • 1
    Take a look at http://stackoverflow.com/questions/955371/partial-view-with-parametrized-prefix-for-controls-names I think you are asking a similar question. – HitLikeAHammer Oct 05 '10 at 23:57

1 Answers1

1

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
  • 3,033
  • 3
  • 30
  • 36
  • 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 Oct 06 '10 at 17:01
  • In terms of ASP MVC a simple way of describing it is basically a partial that handles the prefix for you. – Chao Oct 07 '10 at 11:57