MVC Kendo UI - 将视图数据传递给控制器

时间:2013-06-13 03:47:50

标签: c# asp.net-mvc-4 razor kendo-ui

我是MVC Kendo的新手。我正在创建一个带有一个Datetimepicker,两个组合框和网格的示例程序。当我选择下拉框数据和日期时间选择器日期时,我想知道我想要填充网格。我做了一些工作,但是当我单击“搜索”按钮时,我找不到将所选下拉列表和datetimepicker值发送到控制器的方法。如果有人知道,请告诉我。

Razor View -

@using PortalModels

<table>
    <tr>
        <td>@Html.Label("Date")</td>
        <td></td>
        <td>@Html.Kendo().DatePicker().Name("DTPicker")</td>
    </tr>
    <tr>
        <td>@Html.Label("District")</td>
        <td></td>
        <td>
        @(Html.Kendo().ComboBox()
              .Name("Districts")
              .HtmlAttributes(new { style = "width:300px" })
              .Placeholder("Select category...")
              .DataTextField("CdNm")
              .DataValueField("CdKy")
              .Filter(FilterType.Contains)
              .DataSource(source =>
              {
                  source.Read(read =>
                  {
                      read.Action("GetCascadeDistrict", "MarketInfo");
                  });
              })
        )
        </td>
    </tr>
    <tr>
        <td>@Html.Label("Market")</td>
        <td></td>
        <td>
        @(Html.Kendo().ComboBox()
              .Name("Markets")
              .HtmlAttributes(new { style = "width:300px" })
              .Placeholder("Select product...")
              .DataTextField("CdNm")
              .DataValueField("CdKy")
              .Filter(FilterType.Contains)
              .DataSource(source => {
                  source.Read(read =>
                  {
                      read.Action("GetCascadeMarket", "MarketInfo")
                          .Data("filterMarkets");
                  })
                  .ServerFiltering(true);
              })
              .Enable(false)
              .AutoBind(false)
              .CascadeFrom("Districts")
        )
        <script>
            function filterMarkets() {
                return {
                    categories: $("#Districts").val(),
                    productFilter: $("#Markets").data("kendoComboBox").input.val()
                };
            }
        </script>
        </td>
        <td><input type="submit" id="Submittbn" /></td>
    </tr>
</table> 

@(Html.Kendo().Grid<PortalModels.MarketInfoModel>()
      .Name("grid")
      .Columns(columns =>
      {
          columns.Bound(m => m.ItmNm).Width(400);
          columns.Bound(m => m.Unit).Width(150);
          columns.Bound(m => m.Unit).Width(150);
      })
      .DataSource(dataSource =>
          dataSource.Ajax()
          .ServerOperation(false)
          .Read(read => read.Action("ReadMarketInfoDetails", "MarketInfo").Data("MarketData"))
          .Create(create => create.Action("FamilyDetails", "Home").Data("FamilyData"))
          .PageSize(150)// Action method invoked when the grid needs data

      )
      .Pageable()
      .Sortable() // Enable sorting
)


<script>
    $(function () {
        var MarketGrid = $('#grid').data("kendoGrid");
        $("#Submittbn").click(function () {
            MarketGrid.dataSource.read();
        })
    });

    function MarketData() {
        var EffectiveDtValue = $("#DTPicker").data("kendoDatePicker")
        var DistrictValue = $('#Districts').data("kendoComboBox")
        var MarketValue = $('#Markets').data("kendoComboBox")
        return {
            intEmpky: EffectiveDt.value(),
            intAdrKy: DistrictValue.value(),
            strCode: MarketValue.value()
        }
    }
</script>

2 个答案:

答案 0 :(得分:2)

查看

  • 使用@using (Html.BeginForm())并在表单标记中包含Kendo控件(任何模型属性)很重要
  • @(Html.Kendo().DropDownListFor(m => m.Prospects) //您想使用DropDownListFor绑定到模型
  • 您希望保留分配给DropDownListFor的.Name("Prospects")属性(不是您的问题,但仍然很重要)

查看模型

    public class ViewModelCCTRST
    {
        .
        ..
        public string Prospects { get; set; }
        public IEnumerable<dbProspect> AvailableProspects { get; set; }
        .
        ..
        ...
     }

发布方法

    [HttpPost]
    public ActionResult Index(ViewModelCCTRST model)
    {

        if (ModelState.IsValid)
        {
            string pro = model.Prospects;
            string cnt = model.Countys;
            string twn = model.TownShips;
            string rng = model.Ranges;
            string sct = model.Sections;
            string trt = model.Tracts;

如果您按照这些步骤操作,您应该找到绑定到模型的值!

希望这有帮助

答案 1 :(得分:0)

请尝试使用以下代码段。

查看

<table>
<tr>
    <td>@Html.Label("Date")
    </td>
    <td>
    </td>
    <td>@Html.Kendo().DatePicker().Name("DTPicker")
    </td>
</tr>
<tr>
    <td>@Html.Label("District")
    </td>
    <td>
    </td>
    <td>
        @(Html.Kendo().ComboBox()
          .Name("Districts")
          .HtmlAttributes(new { style = "width:300px" })
          .Placeholder("Select category...")
                   .DataTextField("Text")
                          .DataValueField("Value")
          .Filter(FilterType.Contains)
          .DataSource(source =>
          {
              source.Read(read =>
              {
                  read.Action("GetCascadeDistrict", "Home");
              });
          })
    )
    </td>
</tr>
<tr>
    <td>@Html.Label("Market")
    </td>
    <td>
    </td>
    <td>
        @(Html.Kendo().ComboBox()
          .Name("Markets")
          .HtmlAttributes(new { style = "width:300px" })
          .Placeholder("Select product...")
                  .DataTextField("Text")
                  .DataValueField("Value")
          .Filter(FilterType.Contains)
          .DataSource(source =>
          {
              source.Read(read =>
              {
                  read.Action("GetCascadeMarket", "Home")
                      .Data("filterMarkets");
              })
              .ServerFiltering(true);
          })
          .Enable(false)
          .AutoBind(false)
          .CascadeFrom("Districts")
    )
    </td>
    <td>
        <input type="submit" id="Submittbn" />
    </td>
</tr>

@(Html.Kendo().Grid<MvcApplication1.Models.TestModels>()
  .Name("grid")
  .Columns(columns =>
  {
      columns.Bound(m => m.ID).Width(100);
      columns.Bound(m => m.Name).Width(700);
  })
  .DataSource(dataSource =>
      dataSource.Ajax()
      .ServerOperation(false)
                      .Read(read => read.Action("GridRead", "Home").Data("MarketData"))
      .PageSize(150)

  )
  .Pageable()
  .Sortable())

<强> CONTROLLER

public class HomeController : Controller
{

    public ActionResult GridRead([DataSourceRequest] DataSourceRequest request, string intEmpky, string intAdrKy, string strCode)
    {
        List<TestModels> models = new List<TestModels>();


        for (int i = 1; i < 6; i++)
        {

            TestModels model = new TestModels();
            model.ID = i;
            model.Name = intEmpky + "_" + intAdrKy + "_" + strCode;
            models.Add(model);

        }

        return Json(models.ToDataSourceResult(request));
    }

    [HttpGet]
    public JsonResult GetCascadeDistrict()
    {
        List<SelectListItem> models = new List<SelectListItem>();


        for (int i = 1; i < 6; i++)
        {

            SelectListItem model = new SelectListItem();
            model.Value = i.ToString();
            model.Text = "text" + i;
            models.Add(model);

        }

        return Json(models, JsonRequestBehavior.AllowGet);
    }

    [HttpGet]
    public JsonResult GetCascadeMarket(string categories, string productFilter)
    {
        List<SelectListItem> models = new List<SelectListItem>();


        for (int i = 1; i < 6; i++)
        {

            SelectListItem model = new SelectListItem();
            model.Value = i.ToString();
            model.Text = "text" + i;
            models.Add(model);

        }

        return Json(models, JsonRequestBehavior.AllowGet);
    }

}

注意:请在JS代码中更新“MarketData()”函数。

如果有任何疑虑,请告诉我。