2017-04-05 56 views
1

这是我的控制器的代码。我想从视图中检索数据,并且所有值都为空C#

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Save(Customer customer) 
    { 



     Page.db.Database.ExecuteSqlCommand("INSERT into Customer(Name, Email, PhoneNumber, Address, Quantity) VALUES(@Name, @Email, @PhoneNumber, @Address, @Quantity)", 
      new SqlParameter("@Name", customer.Name), 
      new SqlParameter("@Email", customer.Email), 
      new SqlParameter("@PhoneNumber", customer.PhoneNumber), 
      new SqlParameter("@Address", customer.Address), 
      new SqlParameter("@Quantity", customer.Quantity) 
      ); 

     Page.db.SaveChanges(); 

     return View(); 
    } 

这是我的视图代码。

@using (Html.BeginForm("Save", "Events")) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal buyer-info"> 
     <h1>Purchase</h1> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Customers.Name, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Customers.Name, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Customers.Name, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Customers.Email, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Customers.Email, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Customers.Email, "", new { @class = "text-danger" }) 
      </div> 
     </div> 


     <div class="form-group"> 
      @Html.LabelFor(model => model.Customers.PhoneNumber, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Customers.PhoneNumber, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Customers.PhoneNumber, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Customers.Address, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Customers.Address, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Customers.Address, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Customers.Quantity, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Customers.Quantity, new { htmlAttributes = new { min = 1, max = 5, @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Customers.Quantity, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 

} 

我不知道我在做什么错的,因为我从我的观点提交的数据,但是当我在调试器中运行,

public ActionResult Save(Customer customer) 

“客户”将返回空值。

我第一次这样做,所以如果错误是明显的,请不要愤怒。

+0

在页面上创建的HTML表单元素是什么? “Customer”类是什么样的? “客户”和任何“模型”之间可能存在脱节。“客户”是。 – David

+0

客户在哪里退货?我认为你只是将客户传递给actionMethod,而不是从客户返回的地方。你有没有调试代码,因为我看到你的代码没有错误。 –

+0

感谢David和Adeel,现在客户生病了,现在回答了一个问题。不胜感激 :) – Daniaal

回答

0

好了,所以基本上我控制器继承掉它有对象“页面”基地控制器。在这个对象中,我有这样的事情:

public Entities = new Entities(); 
    public Example1 Example { get; set; } 
    public Example2 Example { get; set; } 
    public Customer customer {get;set; } 

所以我在这里宣布所有的实体,所以我可以用这个来拉数据。 我在做什么是添加使用指令“使用ProjectName.Model.Customer”。但是,在我看来,我使用@model页面,因此我的代码不起作用的原因。

所以这就是我补充说的。

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Save(Page page) 
    { 

     Page.db.Database.ExecuteSqlCommand("INSERT into Customer(Name, Email, PhoneNumber, Address, Quantity) VALUES(@Name, @Email, @PhoneNumber, @Address, @Quantity)", 
      new SqlParameter("@Name",page.customer.Name), 
      new SqlParameter("@Email", page.customer.Email), 
      new SqlParameter("@PhoneNumber", page.customer.PhoneNumber), 
      new SqlParameter("@Address", page.customer.Address), 
      new SqlParameter("@Quantity", page.customer.Quantity) 
      ); 

     Page.db.SaveChanges(); 

     return View(); 
    } 

所以,我做了什么是改变客户客户页面页,我没有改变视图。 DESPITE所有这些,当我运行它时,我得到了“对象引用不等于对象的实例”。所以我不得不补充这一点。

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Save(Page page) 
    { 
     Page.Customers = page.Customers; 

     Page.db.Database.ExecuteSqlCommand("INSERT into Customer(Name, Email, PhoneNumber, Address, Quantity) VALUES(@Name, @Email, @PhoneNumber, @Address, @Quantity)", 
      new SqlParameter("@Name",page.customer.Name), 
      new SqlParameter("@Email", page.customer.Email), 
      new SqlParameter("@PhoneNumber", page.customer.PhoneNumber), 
      new SqlParameter("@Address", page.customer.Address), 
      new SqlParameter("@Quantity", page.customer.Quantity) 
      ); 

     Page.db.SaveChanges(); 

     return View(); 
    } 

正如你可能知道我加入 Page.Customers = page.Customers;然后当我跑了它的所有工作代码和幸福的日子。感谢所有的帮助。

0

FormMethod.Post u必须在剃刀helpe添加

@using (Html.BeginForm("Save", "Events",FormMethod.Post)) 
相关问题