开发者

List AND submit in the same view. How is it possible?

Im currenty develoding a website using mvc 3 razor but have a little problem. I want to make a view that contains a submit form and a list. "Add product" and "List of products" should be in the same view. First I tought that partial views would be the solution but cant figure out how to make it work. Have no idea of how to pass a list of objects (products) to a开发者_Go百科 partial view. Someone has an idea of how to solve my problem?


MainView-Model:

public MainViewModel{

      public IList<Product> AvailableProducts{
           get; set;
      }
      public IList<Product> SelectedProducts{
           get; set;
      }
}

Mainview:

 @Html.RenderPartial("_SelectedProductsPartial", Model.SelectedProducts)  

Partial View:

 @model IList<Product>

Controller:

 public ActionResult MainView(){

 MainViewModel model = new MainViewModel();
 model.AvailableProducts = ...;
 model.SelectedProducts = ...;

 return View(model);


Partials are not required for what you are trying to accomplish, although they can be helpful for keeping your concerns separated.

To answer your question, you can pass an object to a strongly-typed partial view by passing it as the second argument, like so:

@Html.Partial("_MyPartialName", Model.ThingIWantToPass)

In this example this would be a strongly typed partial of the same type as Model.ThingIWantToPass.


The displaying part

// In your action method
IList<ProductViewModel> productListViewModel = new List<ProductViewModel>();
return PartialView(productListViewModel);


//Your view model:


@model IEnumerable<YourNamespace.WebUI.Models.ProductViewModel>

@for(int i = 0; i < Model.Count(); i++){
   // Assuming your product has a name property
   @Html.DisplayFor(model => model.Name);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜