使用viewModel类-ASP.NET MVC5从视图将数据发布到控制器

时间:2014-02-20 10:13:44

标签: c# jquery asp.net-mvc razor

我正在使用ASP.NET MVC-5应用程序。我在Razor局部视图中得到了表单,它需要多个模型,所以我使用ViewModel;将所有类(模型)引用放在以下

public class QualificationViewModel
{    
    public Qualification _Qualification;

    //public Subject _Subject;

    //public FeeScheme _FeeScheme;

}

现在暂时我只使用资格模型来测试我的代码,但是我无法将数据从视图发送到控制器方法。我在控制器方法

中调试时得到null值
  [HttpGet]
    public ActionResult CreateNewQualification()
    {
        return PartialView("PartialQualification_Create");

    }

    [HttpPost]
    public ActionResult CreateNewQualification(QualificationViewModel newQualificationData)
    {
        try
        {
            if (ModelState.IsValid)
            {
                //code here...
            }
        }
        catch (DataException ex)
        {
            ModelState.AddModelError("", "Unable To Create New Qualification!" + ex);
        }

        return RedirectToAction("QualificationHome");
    }

我正在使用jQuery Ajax方法发布数据,我已经成功地为单个模型类发送数据....

function submit_createNewQualification()
{      
    alert(JSON.stringify($("#NewQualificationForm").serializeObject()));

    $.ajax({
        type: "Post",
        url: "/Qualification/CreateNewQualification",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ newQualificationData: $("#NewQualificationForm").serializeObject() }),
        success: function (data) {
            if (data == true) {
                window.location = "/Qualification/QualificationHome";
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
        }
    });

}

我在视图中有以下表格......

 @model DatabaseLayer.ViewModels.QualificationViewModel

  @using (Html.BeginForm("CreateNewQualification", "Qualification", FormMethod.Post, new { id = "NewQualificationForm" }))
    {
        @Html.AntiForgeryToken()
     <div class="form-group">
        @Html.LabelFor(model => model._Qualification.QualificationTitle, new { @class = "control-label col-md-3" })
           <div class="col-md-6">
             @Html.EditorFor(model => model._Qualification.QualificationTitle)
             @Html.ValidationMessageFor(model => model._Qualification.QualificationTitle)
           </div>
           </div>

           <div class="form-group">
              @Html.LabelFor(model => model._Qualification.Active, new { @class = "control-label col-md-3" })
             <div class="col-md-6 custom-checkBox-block-1">
               @Html.EditorFor(model => model._Qualification.Active)
               @Html.ValidationMessageFor(model => model._Qualification.Active)
              </div>
           </div>

     <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
             <input type="submit" value="Create Qualification" onclick="submit_createNewQualification()"  class="btn btn-default" />
         </div>
       </div>
}
非常感谢...

2 个答案:

答案 0 :(得分:5)

试试这个

public class QualificationViewModel
{    
   public Qualification _Qualification {get; set;}

//public Subject _Subject;

//public FeeScheme _FeeScheme;

}

[HttpGet]
public ActionResult CreateNewQualification()
{
    var Model = new QualificationViewModel();
    return PartialView("PartialQualification_Create",model);

}

答案 1 :(得分:2)

你不需要JSON字符串化你的模型,你只需发送你的表单的序列化版本,默认的MVC模型绑定将处理其余的:

data: $("#NewQualificationForm").serialize()