开发者

How could I make my View Model

I have a partial view that contains my form input(textboxes). I have 2 other partial views that use this same form. One for adding a product and one for editing a product.

This form uses a view model(Lets call it CoreViewModel). Now editing product has a couple more fields then adding a product.

I am wondering how can I add these extra fields without them showing up on an add product form?

I cannot add these extra fields to the edit product view they must be in the CoreViewModel otherwise I think styling it will be a nightmare.

I was thinking of having maybe a base class and then for the editing. I would send it a view model that inherits this base class.

Check in the view if the View Model is of this inherited class and not a base class and if it is not a base class render the code.

This way I am not sticking the edit specific code into my CoreViewModel that both the add view and the edit view have access.

I hope this sort of makes sense.

Thanks

Edit

Using Muhammad Adeel Zahid code as I base I think I got it to work

    public class CreateViewModel
    { 
    ......
    ......
    }

    public class EditViewModel:CreateViewModel{
        public string AdditionalProperty1{get;set;}
        public 开发者_运维问答string AdditionalProperty2{get;set;}
    }

Controller

    EditViewModel viewModel = new EditViewModel();
    // add all properties need
    // cast it to base
    return PartialView("MyEditView", (CreateViewModel)viewModel);

View 1

    @Model CreateViewModel
    @using (Html.BeginForm())
    {
        @Html.Partial("Form", Model)
    }

Form View

@Model CreateViewModel
// all properties from CreateView are in here
// try and do a safe case back to an EditViewModel
 @{EditViewModel  edit = Model as EditViewModel ;}

 // if edit is null then must be using this form to create. If it is not null then it is an edit
 @if (edit != null)
 {     // pass in the view model and in this view all specific controls for the edit view will be generated. You will also have intellisense.
       @Html.Partial("EditView",edit)
 }

When you post it back to your Edit action result just take in the EditViewModel and cast it back to your base. Then you will have all the properties as it seems to work


I have often read people advising against such things. They often urge having viewmodel per view (even for edit and create view of same entity for that matter). Again, it all comes down to what you are doing. You may have different data annotations on edit and create view for different validation needs but if they are the same, we probably have a point to use same viewmodel for create and edit.

To solve your scenario I can't figure out couple of options. First, keep a boolean property in your view model telling you if it is and edit or create and conditionally render you properties on view

public class MyViewModel
{
   public string P1{get;set;}
   ....
   public boolean Editing{get;set;}
}

Set Editing property to false in Create ActionResult and to true in Edit ActionReult. This is the simplest method. The second one is little dirtier, but you will feel like using the technology. You can use dynamic behavior of c# 4.0. have your page inherit from dynamic in iherits directive of the page (I use aspx view engine). Then have a create ViewModel:

public class CreateViewModel
{ 
......
......
}
and one Edit ViewModel
public class EditViewModel:CreateViewModel{
    public string AdditionalProperty1{get;set;}
    public string AdditionalProperty2{get;set;}
}

and in your view you can do something like:

<%:if(Model.GetType().Name.ToString() == "EditViewModel"){%>
  <%:Html.Textbox("AdditionalProperty1")%>
    <%:Html.Textbox("AdditionalProperty1")%> 
<%}>

There is a price to pay with dynamic. You lose intellisense and you can't use strongly typed helpers (at least in asp.net MVC 2).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜