如何在运行时将模型传递给partialview?

时间:2016-02-19 05:52:19

标签: asp.net-mvc razor

ViewModel

  public class ModelTypeViewModel
  {
     public virtual  CheckRadioButton CRB { get; set; }


}

控制器

public class M1Controller : Controller
 {
    public CarContext db = new CarContext(); 

    private CheckRadioButton get()
    {
        CheckRadioButton c = new CheckRadioButton();
        c.BrandName = "abc";
        c.type = "xyz";
        return c; 

    }
  public ActionResult Hello ()
    {
        CheckRadioButton s = get();
        ModelTypeViewModel mm = new ModelTypeViewModel(s);


        return View(mm);

    }

查看:(你好)

@model Car.Models.ModelTypeViewModel
@Html.Partial("_Display", Model.CRB) 

部分视图(_Display)

<h1> Hello </h1>

如何每次都将diff模型传递给局部视图?

它出错

  

“System.Web.WebPages.Razor.dll中发生'System.Web.HttpParseException'类型的异常,但未在用户代码中处理”

即使我只传递'模型',也会出现同样的错误 我很困惑

1 个答案:

答案 0 :(得分:0)

将每个按钮放在 Ajax.BeginForm

@using (Ajax.BeginForm("BuyItem", "MsmqTest"}, new AjaxOptions { UpdateTargetId = "msmqpartial" }))
{ <button type="submit">Buy</button>}
@using (Ajax.BeginForm("BuyItem", "MsmqTest" }, new AjaxOptions { UpdateTargetId = "msmqpartial" }))
{
    <button type="submit">Sell</button>
}

&#34; updateTargetId&#34; 是要添加内容的div ID

 public ActionResult BuyItem()
        {
        if(//some condition goes here)
            return PartialView("Partial1",data);
        if(//some condition goes here)
           return PartialView("Partial2",data);
        }