在ASP.NET MVC 4实体框架中处理部分视图和模型绑定的多个实例

时间:2016-01-12 09:18:55

标签: entity-framework asp.net-mvc-4

这是我的控制器代码。

namespace MultipleInstance.Controllers
{
    public class Default1Controller : Controller
    {
     private MVCDemoEntities db = new MVCDemoEntities();

   public ActionResult Index()
    {
        Order ord = new Order();
        //ord.BillingAddress = new Address1();
        //ord.ShippingAddress = new Address();
        return View(ord);
    }

   public ActionResult ProcessForm(Order ord)
   {

       return PartialView("Index");

       }


    }
}

这是我的索引视图。

@using(Html.BeginForm("ProcessForm","Default1",FormMethod.Post))
{
  <h3>Basic Details</h3>
  @Html.Partial("_BasicDetails")
  <h3>Shipping Address</h3>
  @Html.Partial("_Address", 
  new ViewDataDictionary() 
  { 
    TemplateInfo = new TemplateInfo() 
      { HtmlFieldPrefix = "ShippingAddress" } })


      <input type="submit" value="Submit" />
}

这是我的两个部分视图_BasicDetails.cshtml和_Address.cshtml

@model MultipleInstance.Order

<table>
    <tr>
        <td>@Html.LabelFor(m => m.OrderID_)</td>
        <td>@Html.TextBoxFor(m => m.OrderID_)</td>
    </tr>
    <tr>
        <td>@Html.LabelFor(m => m.CustomerID_)</td>
        <td>@Html.TextBoxFor(m => m.CustomerID_)</td>
    </tr>
</table>

这是_Address.cshtml视图。

@model MultipleInstance.Address

<table>
    <tr>
        <td>@Html.LabelFor(m => m.Street1)</td>
        <td>@Html.TextBoxFor(m => m.Street1)</td>
    </tr>
    <tr>
        <td>@Html.LabelFor(m => m.Street2)</td>
        <td>@Html.TextBoxFor(m => m.Street2)</td>
    </tr>
    <tr>
        <td>@Html.LabelFor(m => m.Country)</td>
        <td>@Html.TextBoxFor(m => m.Country)</td>
    </tr>
    <tr>
        <td>@Html.LabelFor(m => m.PostalCode)</td>
        <td>@Html.TextBoxFor(m => m.PostalCode)</td>
    </tr>
</table>

当我运行以上代码后出现错误。部分视图&#39; _BasicDetails&#39;找不到或没有视图引擎支持搜索的位置。任何人都可以在我出错的地方帮助我吗?或者我是否需要在动作方法中更改任何内容?

这是我的模特课

public partial class Order
    {
        public int Id { get; set; }
        public string OrderID_ { get; set; }
        public string CustomerID_ { get; set; }
        public string SelectType { get; set; }
        public Address ShippingAddress { get; set; }

    }



public partial class Address
    {
        public int Id { get; set; }
        public string Street1 { get; set; }
        public string Street2 { get; set; }
        public string Country { get; set; }
        public string PostalCode { get; set; }
    }

提前致谢。

1 个答案:

答案 0 :(得分:1)

这应该有效

@Html.Partial("~Views/shared/_BasicDetails.cshtml", Model)

并且您需要将模型传递给它[并且应该在您的操作中初始化]因为我在您的代码中看到您在部分视图中使用它