Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Ajax-MVC3Razor将数据发送到控制器_Ajax_Asp.net Mvc 3_Razor - Fatal编程技术网

使用Ajax-MVC3Razor将数据发送到控制器

使用Ajax-MVC3Razor将数据发送到控制器,ajax,asp.net-mvc-3,razor,Ajax,Asp.net Mvc 3,Razor,我有一组局部视图,它们在一页中动态更改。PartialView使用ajax加载下一个等。其中一个有20个输入的形式,目前我只发送一个输入,但如何发送其余的输入?我在这个局部视图中看到: var shipping= $("input[name='shipping']:checked").val() $.ajax( { url: '/Home/Payment', type: "POST", data: { 'shippi

我有一组局部视图,它们在一页中动态更改。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的新手

感谢使用JSON模型绑定。 我可以帮你了解情况


简而言之,创建属性名与模型匹配的javascript对象。将其发送到控制器并将其绑定。另外,请注意,数字最好以字符串()的形式发送。

将项目放入表单中,并序列化表单并发送到操作方法

@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" />
}
现在,动作方法参数将成为视图模型类。Mvc模型绑定将发布(序列化)的表单数据映射到视图模型类的实例

[HttpPost]
public ActionResult Save(YourSomeViewModel model)
{
   //check model properties now

  //saved and respond /redirect
}
[HttpPost]
public ActionResult Save(YourSomeViewModel model)
{
   //check model properties now

  //saved and respond /redirect
}