我使用 MVC3 的模型绑定将客户列表绑定到客户搜索结果页面中,并使用 Razor 在 foreach 循环中呈现所有客户。 我的问题是如何将客户对象发送回操作,以节省我再次获取详细信息的麻烦。
以下是我的操作方法签名:
public ActionResult BasketAddCustomer(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呈现的 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>
根据我对 @url.action 的了解,我们只能发送路由值,而不能发送对象本身。
请参考此链接:https://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.action(v=vs.118(.aspx
因此,对于您的问题,您可以将任何唯一值作为 url.helper 中的路由参数发送到控制器操作,并使用 linq query in action 从数据库中获取整个对象。
注意:如果你的要求是,你应该只发送对象,那么你可以将所有控件放在视图中的表单内,并将其作为post方法发送。
希望上面的信息是有帮助的。
谢谢
卡尔西克