ASP.NET /实体框架和MVC

时间:2015-05-04 23:14:56

标签: c# asp.net-mvc entity-framework asp.net-mvc-scaffolding

以下是我的模型示例。 供应收据包含 Block,Grower,的自定义模型以及 SupplyReceiptLine 的集合:

    public class SupplyReceipt
{
    [Key]
    public int Id { get; set; }

    [Required]
    public string LoadNumber { get; set; }
    [Required]
    public DateTime Date { get; set; }
    public string Notes { get; set; }
    [Required]
    public Block Block { get; set; }
    [Required]
    public Contact Grower { get; set; }

    [Required]
    public ICollection<SupplyReceiptLine> SupplyReceiptLines { get; set; }

}

我不认为我的问题是必要的,但这里是我的联系模式(用于种植者)的一个例子:

public class Contact
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string Title { get; set; }
    public string Note { get; set; }

    [Display(Name="Billing Address")]
    public virtual Address BillingAddress { get; set; }
    [Display(Name = "Shipping Address")]
    public virtual Address ShippingAddress { get; set; }

    [Display(Name = "Show as Supplier")]
    public bool IsSupplier { get; set; }
    [Display(Name = "Show as Customer")]
    public bool IsCustomer { get; set; }
    [Display(Name = "Show as Grower")]
    public bool IsGrower { get; set; }
    [Display(Name = "Show as Bin Owner")]
    public bool IsBinOwner { get; set; }

}

现在我遇到了多个问题:

  1. 常规MVC脚手架(实体框架模型)不喜欢Grower,Block或Supply Receipt Lines。我理解供应收据行需要逻辑,但我不明白为什么我必须如此努力地为Grower和Block创建一个下拉选择框。这些类型定义良好(带有Ids和Key属性),数据库中有这些项目的数据。
  2. 任何人都可以解释我的模型中缺少的MVC / EF Scaffolding不明白吗?很多在线教程都会自动显示这一点。我不得不在我的视图中手动放置Html.DropDownListFor,并从我的数据库上下文中传递一个新的SelectList。我还必须更新控制器post方法以包括Grower的整数和Block的整数,并手动查找Grower&amp;在将我的供应收据添加到数据库之前,按ID进行阻止,将它们分配回模型。

    1. 我无法弄清楚这一点。 Supply Receipt Lines是一个ICollection,它应该有多个Supply Receipt Line。如何更新我的创建视图和我的控制器,以便我可以在同一表单中添加多个供应收据行?
    2. 以下是与数字2一起使用的示例屏幕截图:

      In yellow I've highlighted properties of a Supply Receipt Line

      以下是与此“创建”方法相关的其他一些代码,包括编辑器。谢谢你的时间。

      (控制器发布方法)

      [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Create(SupplyReceipt supplyReceipt, int Grower, int Block, ICollection<SupplyReceiptLine> supplyReceiptLines)
          {
      
              // supplyReceiptLines turns up null! So does supplyReceipt.SupplyReceiptLines
      
      
              // Need Grower and Block For Some reason
              supplyReceipt.Grower = db.Contacts.Where(model => model.Id.Equals(Grower)).First();
              supplyReceipt.Block = db.Blocks.Where(model => model.Id.Equals(Block)).First();
      
              if (ModelState.IsValid)
              {
                  db.SupplyReceipts.Add(supplyReceipt);
                  if (supplyReceiptLines != null && supplyReceiptLines.Any())
                  {
                      db.SupplyReceiptLines.AddRange(supplyReceiptLines);
                  }
                  db.SaveChanges();
                  return RedirectToAction("Index");
              }
      
              return View(supplyReceipt);
          }
      

      (创建表单)

      @using (Html.BeginForm())
      {
      @Html.AntiForgeryToken()
      
      <div class="form-horizontal">
          <h4>SupplyReceipt</h4>
          <hr />
          @Html.ValidationSummary(true, "", new { @class = "text-danger" })
          <div class="form-group">
              @Html.LabelFor(model => model.LoadNumber, htmlAttributes: new { @class = "control-label col-md-2" })
              <div class="col-md-10">
                  @Html.EditorFor(model => model.LoadNumber, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model => model.LoadNumber, "", new { @class = "text-danger" })
              </div>
          </div>
      
          <div class="form-group">
              @Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
              <div class="col-md-10">
                  @Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
              </div>
          </div>
      
          <div class="form-group">
              @Html.LabelFor(model => model.Grower, htmlAttributes: new { @class = "control-label col-md-2" })
              <div class="col-md-10">
                  @Html.DropDownListFor(model => model.Grower, new SelectList(Db.Contacts, "Id", "Title"), "Select a Grower", new { @class = "form-control" })
                  @Html.ValidationMessageFor(model => model.Grower, "", new { @class = "text-danger" })
              </div>
          </div>
      
          <div class="form-group">
              @Html.LabelFor(model => model.Block, htmlAttributes: new { @class = "control-label col-md-2" })
              <div class="col-md-10">
                  @Html.DropDownListFor(model => model.Block, new SelectList(Db.Blocks, "Id", "Title"), "Select a Block", new { @class = "form-control" })
                  @Html.ValidationMessageFor(model => model.Block, "", new { @class = "text-danger" })
              </div>
          </div>
      
          <div class="form-group">
              @Html.LabelFor(model => model.Notes, htmlAttributes: new { @class = "control-label col-md-2" })
              <div class="col-md-10">
                  @Html.EditorFor(model => model.Notes, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model => model.Notes, "", new { @class = "text-danger" })
              </div>
          </div>
      
          <hr />
      
          <div id="supplyReceiptLines">
              @Html.EditorFor(model => model.SupplyReceiptLines, "SupplyReceiptLines")
          </div>
      
          <div class="form-group text-right">
              <a href="#" class="-action-add-supply-receipt-line btn btn-primary">Add a row</a>
          </div>
      
          <hr />
      
          <div class="form-group">
              <div class="text-right">
                  <button type="submit" value="Create" class="btn btn-default btn-success">Create Supply Receipt</button>
              </div>
          </div>
      </div>
      }
      

      (编辑模板)

      @model SupplyReceiptLine
      
      @{
      ModelContext Db = new ModelContext();
      }
      <div class="form-group form-inline">
      
      @Html.HiddenFor(model => model.Id)
      
      <div class="col-sm-1">
          @Html.LabelFor(model => model.Qty, htmlAttributes: new { @class = "" })
          @Html.EditorFor(model => model.Qty, new { htmlAttributes = new { @class = "form-control" } })
          @Html.ValidationMessageFor(model => model.Qty, "", new { @class = "text-danger" })
      </div>
      
      <div class="col-sm-1">
          @Html.LabelFor(model => model.Pack, htmlAttributes: new { @class = "" })
          @Html.DropDownListFor(model => model.Pack, new SelectList(Db.Packs, "Id", "Title"), "", new { @class = "form-control" })
          @Html.ValidationMessageFor(model => model.Pack, "", new { @class = "text-danger" })
      </div>
      
      <div class="col-sm-1">
          @Html.LabelFor(model => model.Size, htmlAttributes: new { @class = "" })
          @Html.DropDownListFor(model => model.Size, new SelectList(Db.Sizes, "Id", "Title"), "", new { @class = "form-control" })
          @Html.ValidationMessageFor(model => model.Size, "", new { @class = "text-danger" })
      </div>
      
      <div class="col-sm-3">
          @Html.LabelFor(model => model.Variety, htmlAttributes: new { @class = "" })
          @Html.DropDownListFor(model => model.Variety, new SelectList(Db.Varieties, "Id", "Title"), "", new { @class = "form-control" })
          @Html.ValidationMessageFor(model => model.Variety, "", new { @class = "text-danger" })
      </div>
      
      <div class="col-sm-3">
          @Html.LabelFor(model => model.Grade, htmlAttributes: new { @class = "" })
          @Html.DropDownListFor(model => model.Grade, new SelectList(Db.Grades, "Id", "Title"), "", new { @class = "form-control" })
          @Html.ValidationMessageFor(model => model.Grade, "", new { @class = "text-danger" })
      </div>
      
      <div class="col-sm-3">
          @Html.LabelFor(model => model.BinOwner, htmlAttributes: new { @class = "" })
          @Html.DropDownListFor(model => model.BinOwner, new SelectList(Db.Contacts, "Id", "Title"), "", new { @class = "form-control" })
          @Html.ValidationMessageFor(model => model.BinOwner, "", new { @class = "text-danger" })
      </div>
      
      </div>
      

0 个答案:

没有答案