2017-07-24 72 views
0

尝试保存记录,以便该表(提议)将其保存在另一个表的ID(客户端)下。因此,创建链接位于“详细信息”视图模板上,因为当我单击该表(客户端)的详细信息时,会有链接在其下创建新记录(提案)。创建与另一个表的ID相关联的记录 - 不保存

我的确如何在另一张桌子上完成了这个工作,其中客户端属于另一张桌子,并且有一个创建记录的链接,以便在创建时应打开创建视图模板并显示关联的ID并保存它提交时(而不是脚手架下拉选择选项)。

HTML(概念)

CREATE link (which creates a proposal record under the client, see VIEW below) 

控制器:

public ActionResult Create(int? clientId) 
    { 
     ViewBag.Title = "Create a Proposal"; 
     ViewBag.PanelColor = "success"; 

     ViewBag.ClientId = new SelectList(db.Clients, "ClientId", "FullName"); 
     var proposal = new Proposal(); 
     proposal.ClientId = (int)clientId; 
     return View(proposal); 
    } 


    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(Proposal proposal) 
    { 
     ViewBag.Title = "Create a Proposal"; 
     ViewBag.PanelColor = "success"; 

     if (ModelState.IsValid) 
     { 
      var client = db.Clients.Single(c => c.ClientId == proposal.ClientId); 
      client.Proposals.Add(proposal); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     ViewBag.ClientId = new SelectList(db.Clients, "ClientId", "FullName", proposal.ClientId); 
     return View(proposal); 
    } 

VIEW(其中POST发生开始视图)

@model AppName.Models.Client 
... 
<a href="/proposal/[email protected]">CREATE</a> // yes I could use the helper as well, but that's not the point 

VIEW(实际创建视图 - 它只是香草脚手架视图)

     @using (Html.BeginForm()) 
        { 
         <div class="form-horizontal"> 
          @Html.AntiForgeryToken() 

          <div> 

           @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
           <fieldset> 
            <legend>Client</legend> 
            <div class="form-group"> 
             <label class="control-label col-md-2">Client Full Name</label> 
             <div class="col-md-10"> 
              @Html.DropDownList("ClientId", null, htmlAttributes: new { @class = "form-control" }) 
              @Html.ValidationMessageFor(model => model.ClientId, "", new { @class = "text-danger" }) 
             </div> 
            </div> 
           </fieldset> 

           <fieldset> 
            <legend>Proposal Details</legend> 
            <div class="form-group"> 
             <label class="control-label col-md-2">Design</label> 

             @*@Html.LabelFor(model => model.FullName, htmlAttributes: new { @class = "control-label col-md-2" })*@ 
             <div class="col-md-5"> 
              @Html.EditorFor(model => model.Carrier, new { htmlAttributes = new { @class = "form-control", @placeholder = "Carrier" } }) 
              @Html.ValidationMessageFor(model => model.Carrier, "", new { @class = "text-danger" }) 
             </div> 
             <div class="col-md-5"> 
              @Html.EditorFor(model => model.Product, new { htmlAttributes = new { @class = "form-control", @placeholder = "Product" } }) 
              @Html.ValidationMessageFor(model => model.Product, "", new { @class = "text-danger" }) 
             </div> 
            </div> 



            <div class="form-group"> 
             <label class="control-label col-md-2">Benefit Amount</label> 


             <div class="col-md-10"> 
              @Html.EditorFor(model => model.BenefitAmount, new { htmlAttributes = new { @class = "form-control", @placeholder = "Benefit amount" } }) 
              @Html.ValidationMessageFor(model => model.BenefitAmount, "", new { @class = "text-danger" }) 
             </div> 
            </div> 

            <div class="form-group"> 
             <label class="control-label col-md-2">Date Created</label> 

             <div class="col-md-10"> 
              @Html.EditorFor(model => model.DateCompleted, new { htmlAttributes = new { @class = "form-control", Value = System.DateTime.Now.ToString("yyyy-MM-dd") } }) 
              @Html.ValidationMessageFor(model => model.DateCompleted, "", new { @class = "text-danger" }) 
             </div> 
            </div> 

           </fieldset> 

           <div class="form-group"> 
            <div class="col-md-12"> 
             <input type="submit" value="Create" class="btn btn-default" /> 
            </div> 
           </div> 
          </div> 
         </div> 
        } 

ERROR

Not savin 

回答

1

所有你在这里是一个简单的HTML链接,默认情况下将发出指定href GET请求。那大概会称之为你在这里发布的唯一回应GET的第一个动作Create。此操作会将该ID分配给ClientId,但重要的是不会保存任何内容,所以我不确定您为什么期待其他事情发生。

如果你想要打开POST动作,你需要在表单中实际发布数据,这通常需要一个实际的表单标签和一个提交按钮。如果你想使用一个简单的链接,你需要将一个点击事件处理程序绑定到手动发布表单(这很愚蠢)或通过AJAX提交帖子,通过收集表单数据并提交它全部一次到达在该AJAX调用中指定的URL。

您可能会试图在GET操作中添加某种保存功能,但那会是一个错误。 GET不应该修改任何内容。所有原子动作应通过POST或更合适的动词(如PUT,DELETE等)处理。

最后,您应该避免使用Bind。这是有史以来最糟糕的事情,只会让你的代码变得脆弱而容易出错。有关更多信息,请参阅:Bind is Evil

+0

进行了编辑。基本上,在我看来,有一个链接来创建一个记录。然后这个链接打开'创建表单'。在URL上确保这个视图在特定的ID下。但是当我点击提交按钮时。不保存。 – mythoslife

+0

那么,发布后的操作非常简单。如果没有保存,唯一合乎逻辑的原因是'ModelState.IsValid'为false。如果你在页面上没有看到任何特定的错误,你可能想从'@ Html.ValidationSummary(true)'中删除'true'参数,这会导致它显示所有错误,而不仅仅是模型级错误。可能有一个属性,你没有期望有验证错误,确实有验证错误。 –

+0

DId没有想到是的,我做了调试。我忘了我添加了一列并添加了一个输入框。现在它正在节省。 – mythoslife

相关问题