通过ajax.beginform传递模型

时间:2014-02-21 21:19:46

标签: javascript jquery ajax asp.net-ajax asp.net-mvc-5

型号:

 public class AdminViewModel {
    public AdminViewModel()
    {

    }
    public Reseller CurrentReseller { get; set; }

    public IEnumerable<Reseller> AllResellers { get; set; }
    public ResellerWebSetting CurrentResellerWebSettings { get; set; }

    public ResellerPresaleMessage PreSaleMessage { get; set; }
}

控制器:

public ActionResult RenderPartials(AdminViewModel model)
    {
         //do stuff with model
         return PartialView("_SellerWebSettings", model);//sending model back with more info
    }

查看:

@model CellularFlashingMVC.Models.ViewModels.AdminViewModel
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}

@using (Ajax.BeginForm("RenderPartials", "Admin", new { @model = Model  }, new AjaxOptions {
        InsertionMode = InsertionMode.Replace,
        LoadingElementDuration = 500,
        LoadingElementId = "AjaxLoading",
        HttpMethod = "POST"
    }, new { id = "DDLReseller" })) {
        <div>
            @(Html.Kendo().DropDownListFor(rs => rs.CurrentReseller.RouteName)
            .Name("CurReseller")
            .DataTextField("Name")
            .DataValueField("Name")
            .Events(e => e.Change("OnCurResellerChanged"))
            .DataSource(source => {
                source.Read(read => {
                    read.Action("GetResellers", "Admin");
                });
            }).OptionLabel("-- Select a Reseller --"))
            //@Html.Hidden("model", Model) this didn't work either
        </div>
    }

 <script>



function OnCurResellerChanged() {
      $("#DDLReseller").submit();
  }
 </script>

为什么我不了解或理解在我的控制器方法中,“model”属性始终为null?我已经尝试过隐藏的字段,我也试过为$ .ajax做手写的JS并让它序列化它仍然是null。我不确定发生了什么......

这就是我对自定义JS所拥有的。

 $.ajax({
                url: $("#CurReseller").attr('action'),
               type: $("#CurReseller").attr('method'),
                data: $("#CurReseller").serialize(),
               success: function (data) {
                   alert(data.Status);
                }

0 个答案:

没有答案