获取ajax成功数据的某些属性并将其绑定到视图

时间:2018-07-02 12:40:44

标签: c# ajax asp.net-mvc model-binding

我有以下内容:

public class CarModelList
{
    public CarModel CarsSea { get; set; }
    public CarModel CarsAir { get; set; }
    public CarModel CarsSoil { get; set; }
}

CarModel如下:

public class CarModel
{
        public long Id { get; set; }

        public string Color { get; set; }

        public List<SelectItem> ColorList { get; set; }

        public string Seats { get; set; }

        public string Model { get; set; }

        public string Year { get; set; }

}

单击某些复选框后,新的CarModel CarsAir将添加到CarModelList。这是通过ajax调用完成的:

var obj = $('#form0').serialize();
             var jsonObj = @Html.Raw(Json.Encode(Model))
             console.log(jsonObj);
             $.ajax({
             url: "@Url.Action("AddAirModel", "Quote")",
             //contentType: "application/json",
             data: jsonObj,
             dataType: "json",
             type: "POST",
             success: function (data, textStatus, jqXHR) {
                 //data.appendTo("#AirQuote");
                 console.log(data.language);
                 $("#AirQuote").show();

             },
             error: function (jqXHR, statusText, errorText) {
                 alert('Error: ' + errorText)
             }

我在Controller中的操作是:

    [HttpPost]
    public ActionResult AddAirModel(CarModelList listCars)
    {
        listCars.CarsAir = new CarModel();
        listCars.CarsAir.TypeOfService = "3";
        ConfigureViewModel(listCars.CarsAir);
        return Json(new { data = listCars }, JsonRequestBehavior.AllowGet);
    }

此正确添加会在CarModelList中初始化新的CarsAir,并在数据中返回CarModelList listCars。 但是,我现在想将CarsAir绑定到视图。我有以下内容:

    if (Model.QuoteModelAir != null)
    {
    <div id="AirQuote">
        <div>
                        @Html.LabelFor(model => model.QuoteModelAir.Seats)
                        @Html.EditorFor(model => model.QuoteModelAir.Seats)
        </div>
        ....so on for all the properties of CarsAir.

    </div>
    }

那么我如何将data.CarsAir从ajax结果绑定到视图?

0 个答案:

没有答案