我有一组在页面中动态更改的PartialView。 PartialView使用ajax加载下一个等等。其中一个有20个输入的形式,目前我只是发送一个输入,但我怎么能发送其余的输入?我在这个部分视图中有这个:
var shipping= $("input[name='shipping']:checked").val()
$.ajax(
{
url: '/Home/Payment',
type: "POST",
data: { 'shipping': shipping},
success: function (data) {
$("#cart").html(data);
}
});
这在控制器中:
public ActionResult Payment(string shipping)
{
**do stuff*
return PartialView("NextPartialView");
}
控制器接收表单的每个输入,但正如我之前所说,其中有20个。有没有办法使用另一种技术发送数据来保持代码的清洁和可读性?
我是MVC和razor的新手
由于
答案 0 :(得分:1)
使用JSON模型绑定。 This post可以帮助您了解情况。
简而言之,创建具有匹配模型的属性名称的javascript对象。将它发送到控制器,它将被绑定。另请注意,数字最好以字符串形式发送(look at this)。
答案 1 :(得分:0)
将您的项目放在表单中并序列化表单并发送到操作方法。
@model YourSomeViewModel
@using(Html.Beginform())
{
FirstName : @Html.TextBoxFor(x=>x.FirstName)
LastName: @Html.TextBoxFor(x=>x.LastName)
Location : @Html.TextBoxFor(x=>x.Location)
Is Available to hire @Html.CheckBoxFor(x=x.IsAvailable)
<input type="submit" id="btnSubmit" />
}
现在在你的脚本中
$(function(){
$("#btnSubmit").click(function(e){
$.post("@Url.Action("Save","YourControllerName")",
$(this).closest("form").serialize(), function(data){
//do something with the response
});
});
});
现在,您的action方法参数将成为您的视图模型类。 Mvc Model绑定会将已发布(序列化)的表单数据映射到视图模型类的实例。
[HttpPost]
public ActionResult Save(YourSomeViewModel model)
{
//check model properties now
//saved and respond /redirect
}