如何在MVC2中显示和更新数据

时间:2010-03-07 05:08:35

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

产品
产品编号
产品名称

ProductSupplier
ProductSupplierId
产品编号 供应商ID

供应商
供应商ID
SupplierName

我的数据库中有上述3个表,ProductSupplier是查找表。每个产品都可以有很多供应商。我正在使用实体框架。

使用Web表单可以非常轻松地在网页上显示产品并将转发器与供应商信息绑定。

此外,使用Web Forms很容易添加新产品和供应商,连接似乎很容易。

如何在MVC中执行此类功能?
在下面的创建视图中,我希望能够添加供应商 有没有更好的方法,我可能会在这里失踪?
这就是我用Web Forms做的方式。

除了下面的代码,我完全迷失了。我可以在列表中显示数据,还可以显示每个产品的供应商,但如何添加和编辑。我应该把它分成不同的观点吗?使用Web窗体,我可以在一个页面中完成所有操作。

namespace MyProject.Mvc.Models
{
  [MetadataType(typeof(ProductMetaData))]
  public partial class Product
  {
    public Product()
    {
      // Initialize Product
      this.CreateDate = System.DateTime.Now;
    }
}

public class ProductMetaData
{
  [Required(ErrorMessage = "Product name is required")]
  [StringLength(50, ErrorMessage = "Product name must be under 50 characters")]
  public object ProductName { get; set; }

  [Required(ErrorMessage = "Description is required")]
  public object Description { get; set; }
}

public class ProductFormViewModel
{
  public Product Product { get; private set; }
  public IEnumerable<ProductSupplier> ProductSupplier { get; private set; }

  public ProductFormViewModel()
  {
      Product = new Product();
  }

  public ProductFormViewModel(Product product)
  {
      Product = product;
      ProductSupplier = product.ProductSupplier;
  }
 }
}

ProductRepository

public Product GetProduct(int id)
{
  var p = db.Product.FirstOrDefault(por => por.ProductId == id);
  p.ProductSupplier.Attach(p.ProductSupplier.CreateSourceQuery().Include("Product").ToList());

        return p;
    }

产品创建视图

<%@ 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>

<% } %>

1 个答案:

答案 0 :(得分:1)

最好的方法是创建一个ViewModel,这是一个由产品实例,productSupplier实例和供应商实例组成的模型。当您创建一个视图时,您强烈键入新的ViewModel,然后创建所需的所有字段,如上所述。

通过添加和编辑来简化复杂性我发现创建一个EditorTemplates文件夹,然后你的模型自己的编辑器模板有帮助,当涉及到创建编辑器模板时,你会有这样的事情:

public ActionResult EditProduct(int id)
{
  var model = new ProductViewModel{ productInfo = product.SingleOrDefault(x => x.prodid==id,
  Suppliers = Suppliers.all(),
  ProductSuppliers = ProdSuppliers.All()
)};
return(model);
}

然后您只需要处理需要选择的供应商,这是您的productInfo中的ID,再次使用EditorTemplate来保存重复的工作和错误。 上面应该给你一个大概的从哪里开始这个,那就是我在这里正确的轨道?

如果我离开这里,请澄清

相关问题