表产品 产品编号 产品名称
表供应商 供应商ID 产品编号 SupplierName
当我创建新产品时,我希望在同一视图中有一个文本框来输入供应商。这是一个好习惯吗?由于产品可以有许多供应商,我希望能够从同一视图添加更多供应商记录。 我该怎么做?
我想知道我在aspx页面中放了什么?
如果我输入类似<%= Html.TextBoxFor(model => model.Supplier)%>我看到一个带有System.Data.Objects.DataClasses.EntityCollection`1 [MyProject.Mvc.Models.Supplier]的文本框。
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<MyProject.Mvc.Models.ProductFormViewModel>" %>
<%= Html.ValidationSummary("Please correct the errors and try again.") %>
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%= Html.LabelFor(model => model.Product.ProductId) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Product.ProductId) %>
<%= Html.ValidationMessageFor(model => model.Product.ProductId) %>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.Product.ProductName) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Product.ProductName) %>
<%= Html.ValidationMessageFor(model => model.Product.ProductName) %>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.Product.Description) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Product.Description) %>
<%= Html.ValidationMessageFor(model => model.Product.Description) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
ProductViewModel
public class ProductFormViewModel
{
public Product Product{ get; private set; }
public IEnumerable<Supplier> Supplier { get; private set; }
public ProductFormViewModel()
{
Product = new Product();
}
public ProductFormViewModel(Product product)
{
Product = product;
Supplier = product.Supplier;
}
}
答案 0 :(得分:1)