MVC剑道UI -传递视图数据到控制器

本文关键字:视图 数据 控制器 剑道 UI MVC | 更新日期: 2023-09-27 18:18:03

我是MVC剑道新手。我正在创建一个示例程序与一个日期时间选择器,两个组合框和网格。我想知道的是,当我选择dropdownboxes数据和datetime选择器时,我想填充Grid。我已经做了一些工作,但是当我单击Search按钮时,我找不到发送选定的下拉菜单和datetimepicker值到控制器的方法。如果有人知道,请告诉我。

Razor视图——

@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>

MVC剑道UI -传递视图数据到控制器

View

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

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

Post方法

    [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;

如果您遵循这些步骤,您应该会发现您的值绑定到您的模型!

希望能有所帮助

请尝试使用下面的代码片段。

<<p> 视图/strong>
<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())
控制器

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()"函数。