这是我的控制器的代码。我想从视图中检索数据,并且所有值都为空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)
“客户”将返回空值。
我第一次这样做,所以如果错误是明显的,请不要愤怒。
在页面上创建的HTML表单元素是什么? “Customer”类是什么样的? “客户”和任何“模型”之间可能存在脱节。“客户”是。 – David
客户在哪里退货?我认为你只是将客户传递给actionMethod,而不是从客户返回的地方。你有没有调试代码,因为我看到你的代码没有错误。 –
感谢David和Adeel,现在客户生病了,现在回答了一个问题。不胜感激 :) – Daniaal