为什么我的控制器获取地址,电话,用户名,电子邮件的空值?
当我调试时,Payment2 POST已从视图中收到id并具有值。
我的控制器:
[HttpGet]
public ActionResult Payment2(int id)
{
model md = new model();
var pm = new paymentmodel();
var cart = Session[CartSession];
if (cart != null)
{
md.v1 = (List<CartItemModel>)cart;
}
md.v2 = pm.userdetails(id);
return View(md);
}
[HttpPost]
public ActionResult Payment2(int id, User c)
{
var cart = (List<CartItemModel>)Session[CartSession];
foreach (var item in cart)
{
if (item.quantity > item.Product.suspended)
{
return Redirect("/loi-so-luong");
}
}
var oder = new HoaDon();
oder.Date = DateTime.Now;
oder.Address = c.Address;
oder.Phone = c.Phone;
oder.TenNguoiNhan = c.UserName;
oder.E_mail = c.Email;
}
return Redirect("/hoan-thanh");
}
NULL Address
,Phone
,UserName
,Email
我的model.cs:
public class model
{
public List<CartItemModel> v1 { set; get; }
public List<get_customer_Result> v2 { set; get; }
}
我的付款模式:
public class paymentmodel
{
KINHDOANHVLXDEntities ke = new KINHDOANHVLXDEntities();
public List<get_customer_Result> userdetails(int id)
{
var ln = ke.get_customer(id);
return ln.ToList();
}
}
我的索引视图:
@model List<thuctaplan2.Models.CartItemModel>
@using thuctaplan2.Common
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
var session = (UserLogin)Session[CommonConstants.USER_SESSION];
}
<a href="@Url.Action("Payment2", new { id = session.id_NguoiDung})" class="btn">Thanh Toán</a>
我的付款2查看:
@model thuctaplan2.Models.model
@{
ViewBag.Title = "Thanh Toán";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("Payment2", "Cart", FormMethod.Post))
{
foreach (var item in Model.v2)
{
<div class="form-group">
<label>Name</label>
<label>@item.UserName</label>
</div>
<div class="form-group">
<label>Phone</label>
<label>@item.Phone</label>
</div>
<div class="form-group">
<label>Address</label>
<label>@item.Address</label>
</div>
<div class="form-group">
<label>E-mail</label>
<label>@item.Email</label>
</div>
}
<button type="submit" class="btn">Send</button>
}
答案 0 :(得分:0)
根据您的代码,这里发生的事情是,当您渲染视图时,Form中的所有控件都是空的或没有值,简而言之,null。为什么?因为您只在表单中呈现标签html元素。
要获得值,请添加如下文本框:
@Html.TextBoxFor(modelItem => item.UserName, new { @class = "form-control" })
因此,当您提交表单时,User
模型属性(如UserName
)将具有值。如果这只是所有标签,则不会将任何值传递给您的控制器。