【发布时间】:2017-06-09 00:27:32
【问题描述】:
我使用 MVC3 的模型绑定将客户列表绑定到客户搜索结果页面,并使用 Razor 在 foreach 循环中呈现所有客户。我的问题是如何将客户对象发送回操作,以免我不得不再次获取详细信息。
下面是我的动作方法签名:
public ActionResult BasketAddCustomer(Customer customer)
Customer 对象非常大,即。很多领域
下面是视图的简化版本,它呈现每个客户并具有选择每个客户的按钮。
@model WebUI.Models.SearchModel
@foreach (var customer in Model.Customers)
{
<h5>@customer.FirstName @customer.LastName</h5>
<button onclick="window.location.href = '@Url.Action("BasketAddCustomer", "Cust", customer)';">Select customer</button>
}
这样做的问题是传递到操作中的客户似乎充满了空值。
@URL.Action 呈现的 html 如下所示,看起来不错,但只有一些客户字段,而不是全部。客户是否过于复杂而无法以这种方式分解?有没有更好的方法?
<button onclick="window.location.href =
'/Test/Cust/BasketAddCustomer?BirthDate=01%2F01%2F0001%2000%3A00%3A00&PrimaryEmailFlag=False&PrimaryEmailDate=01%2F01%2F0001%2000%3A00%3A00&PrimaryEmailID=0&PrimaryPhoneFlag=False&PrimaryPhoneDate=01%2F01%2F0001%2000%3A00%3A00&PrimaryPhoneID=0&WifiConnected=False';" >Select customer</button>
【问题讨论】:
-
您可以只传递客户的 id 并在您的操作中使用传递的 id 从数据库中选择客户,或者您可以使用 ajax 请求传递对象
-
啊,是的,ajax 请求可能就是这样做的方式,非常感谢。
标签: c# asp.net-mvc razor