2016-05-30 44 views
1

我有三个CaseCade组合框,它工作得很好,我 的问题是开始时我希望保存选定的ID, 我知道如何让选择的ID这将是这个样子,和我有与用的ViewModels工作在剑道UI ASP.net MVC

var countries = $("#countries").data("kendoDropDownList") 
countries.value() 

没问题,我不知道如何设置,我在我的观点中定义我上面的视图模型这个值,我不知道怎样将视图模型传递给POST函数。

这是我的看法

@using System.Web.Optimization 
@using Kendo.Mvc.UI 
@model Mfr.Admin.Models.Address.CreateViewModel 
@{ 
    Layout = "~/Views/Shared/_Main.cshtml"; 
} 
< 
<div class="demo-section k-content"> 
<h4>Countries:</h4> 
@(Html.Kendo().DropDownListFor(m=>m.CountryId) 
     .HtmlAttributes(new {style = "width:100%"}) 
     .OptionLabel("Select Country...") 
     .DataTextField("Title") 
     .DataValueField("Id") 
     .DataSource(source => 
     { 
      source.Read(read => 
      { 
       read.Action("GetCascadeCountries", "Address"); 
      }); 
     }) 
    ) 

<h4 style="margin-top: 2em;">states:</h4> 
@(Html.Kendo().DropDownListFor(m=>m.StateId) 
     .HtmlAttributes(new {style = "width:100%"}) 
     .OptionLabel("Select state...") 
     .DataTextField("stateTitle") 
     .DataValueField("stateId") 
     .DataSource(source => 
     { 
      source.Read(read => 
      { 
       read.Action("GetCascadeStates", "Address") 
        .Data("filterStates"); 
      }) 
       .ServerFiltering(true); 
     }) 
     .Enable(false) 
     .AutoBind(false) 
     .CascadeFrom("CountryId") 
    ) 
<script> 
    function filterStates() { 
     return { 
      countries: $("#CountryId").val() 
     }; 
    } 
</script> 

<h4 style="margin-top: 2em;">cities:</h4> 
@(Html.Kendo().DropDownListFor(m=>m.CityId) 
     .HtmlAttributes(new {style = "width:100%"}) 
     .OptionLabel("Select city...") 
     .DataTextField("cityTitle") 
     .DataValueField("cityId") 
     .DataSource(source => 
     { 
      source.Read(read => 
      { 
       read.Action("GetCascadeCities", "Address") 
        .Data("filterCities"); 
      }) 
       .ServerFiltering(true); 

     }) 
     .Enable(false) 
     .AutoBind(false) 
     .CascadeFrom("StateId") 

    ) 
<script> 
    function filterCities() { 
     return { 
      cities: $("#StateId").val() 
     }; 
    } 
</script> 
<button class="k-button k-primary" id="get" style="margin-top: 2em; float:  right;">Save</button> 

</div> 
<script> 

$(document).ready(function() { 

    $("#get").click(function() { 
     // I want To call My post function Here, and pass viewmodel with initialized values to that 
     // I Suppose It will be something like this 
     //but I dont know how to set values to view model 
     //$.ajax({ 
     // url: "@Html.Raw(Url.Action("Create", "Address"))", 
     // type: "POST", 
     // dataType: "json" 
     //)}; 
    }); 
}); 

</script> 
    <style> 
    .k-readonly { 
    color: gray; 
} 

这是我的拯救行动

这里的地址未初始化

 [HttpPost] 
     public ActionResult Create(CreateViewModel address) 
    { 
     if (address == null) 
      throw new ArgumentNullException(nameof(address)); 

     var addressModel = new Address() 
     { 
      Description = address.Description, 
      CityId = address.CityId, 
      StateId = address.StateId, 
      CountryId = address.CountryId, 
      UserApplicationId = User.Identity.GetUserId<int>() 
     }; 

     _addressRepository.Add(addressModel); 
     _addressRepository.Complete(); 

     return Json(""); 

    } 

这是视图模型

public class CreateViewModel 
    { 

    public int Id { get; set; } 
    public int UserApplicationId { get; set; } 

    public int CountryId { get; set; } 

    public int StateId { get; set; } 

    public int CityId { get; set; } 
    public string Description { get; set; } 

} 

回答

0

你只需要发送JSON回控制器

var countries = $("#countries").data("kendoDropDownList") 
var countryId = countries.value(); 
var id = ..... //and so on 

var data = {'Id': id, 'CountryId': countryId, /*ad so on*/ } 

... 
data: JSON.stringify(data) 
... 

收集你需要的所有数据,把它放在JSON对象字符串化,如果JSON属性对应正确地属性模特,它会自动绑定到控制器的参数。