如何将剑道 Ui 下拉选择值的 ID 传递到控制器上? [英] how to pass the ID of the kendo Ui dropdown selected value onto the controller?

查看:14
本文介绍了如何将剑道 Ui 下拉选择值的 ID 传递到控制器上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个使用 KENDO UI Dropdown 的应用程序.问题是我想将视图中的值更新到数据库中.在下拉列表中选择任何值时,与其关联的 ID 应传递到控制器以进行所需的数据库更新.但在这里,下拉列表中任何选择的 ID 都会将null"作为值传递给控制器​​.

我的观点

@using Kendo.Mvc.UI@model ExamplekendoDropdown.Models.FacilityGroup@{ViewBag.Title = "设施组";}<h2>设施组</h2>@using (Html.BeginForm()){@Html.ValidationSummary(true)<字段集><legend>FacilityGroup</legend><div id="RegionName" class="editor-label">@Html.LabelFor(model => model.RegionId)

<div class="editor-field">@* @Html.EditorFor(model => model.RegionName)*@@(Html.Kendo().DropDownList().Name("区域").DataTextField("区域名称").DataValueField("RegionId").DataSource(源=>{source.Read(读=>{read.Action("GetRegion", "Fill");});}))@Html.ValidationMessageFor(model =>model.RegionId)

<div id="休息">@*<form method="post" action='@Url.Action("Submit")' style="width:45%"><div>@(Html.Kendo().Upload().Name("文件"))<p><input type="submit" value="Submit" class="k-button"/></p>

</form>*@<div class="editor-label">@Html.LabelFor(model => model.FacityGroupName)

<div class="editor-field">@Html.EditorFor(model => model.FacityGroupName)@Html.ValidationMessageFor(model => model.FacityGroupName)

<div class="editor-label">@Html.LabelFor(model => model.status)

<div class="editor-field">@Html.EditorFor(model => model.status)@Html.ValidationMessageFor(model =>model.status)

<div class="editor-label">@Html.LabelFor(model => model.CreationDate)

<div class="editor-field">@* @Html.EditorFor(model => model.CreationDate)*@@(Html.Kendo().DatePicker().Name("日期选择器").Value("17/08/2011").HtmlAttributes(new { style = "width:150px" }))@Html.ValidationMessageFor(model =>model.CreationDate)

<p><输入类型=提交"值=创建"/></p>

</fieldset>}<div>@Html.ActionLink("查看列表", "列表")

<div>@Html.ActionLink("返回列表", "关于")

<script type="text/javascript">$(document).ready(function () {$("#RegionName").click(function () {$("#Rest").show();});});@section 脚本 {@Scripts.Render("~/bundles/jqueryval")}

控制器:-

使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 System.Web.Mvc;使用 ExamplekendoDropdown.Models;命名空间 ExamplekendoDropdown.Controllers{公共类 FacilityGroupController : 控制器{////获取:/设施组/公共 ActionResult FacilityGroup(){返回视图();}[HttpPost]公共 ActionResult FacilityGroup(FacilityGroup objadd){AMIEntities1 obj1 = 新的 AMIEntities1();设施 objtbl = 新设施();objtbl.RegionId = Convert.ToInt16(objadd.RegionId);objtbl.FacityGroupName = objadd.FacityGroupName.ToString();objtbl.Status = objadd.status;objtbl.CreationDate = objadd.CreationDate;obj1.AddToFacilities(objtbl);obj1.SaveChanges();obj1.AcceptAllChanges();返回视图();}}}

请帮忙!!

解决方案

我建议您使用 DropDownListFor() 并确保 .Name 与属性名称匹配.在你的情况下:

 @(Html.Kendo().DropDownListFor(model => model.RegionId).Name("RegionId").DataTextField("区域名称").DataValueField("RegionId").DataSource(源=>{source.Read(读=>{read.Action("GetRegion", "Fill");});}))

I am trying to create an application where i am using KENDO UI Dropdown. The Problem is i want to update the values from my view into the database . On selection of the any value in the dropdown the ID associated with it should be passed onto the controller for the required database updation. But here the ID for any selection of the dropdown list passes "null" as the value onto the controller .

My view

@using Kendo.Mvc.UI
@model ExamplekendoDropdown.Models.FacilityGroup

@{
    ViewBag.Title = "FacilityGroup";
}

<h2>FacilityGroup</h2>

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>FacilityGroup</legend>

        <div id="RegionName"  class="editor-label">
            @Html.LabelFor(model => model.RegionId)
        </div>
        <div class="editor-field">
           @* @Html.EditorFor(model => model.RegionName)*@
           @(Html.Kendo().DropDownList()
          .Name("Region")
          .DataTextField("RegionName")
          .DataValueField("RegionId")
          .DataSource(source =>
          {
              source.Read(read =>
              {
                  read.Action("GetRegion", "Fill");
              });

          })
    )
            @Html.ValidationMessageFor(model => model.RegionId)
        </div>
     <div id="Rest">
@*<form method="post" action='@Url.Action("Submit")' style="width:45%">
    <div>
        @(Html.Kendo().Upload()
            .Name("files")
        )
        <p>
            <input type="submit" value="Submit" class="k-button" />
        </p>
    </div>
</form>*@

        <div class="editor-label">
            @Html.LabelFor(model => model.FaclityGroupName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.FaclityGroupName)
            @Html.ValidationMessageFor(model => model.FaclityGroupName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.status)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.status)
            @Html.ValidationMessageFor(model => model.status)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CreationDate)
        </div>
        <div class="editor-field">
          @*  @Html.EditorFor(model => model.CreationDate)*@
          @(Html.Kendo().DatePicker()
              .Name("datepicker")
              .Value("17/08/2011")
              .HtmlAttributes(new { style = "width:150px" })
        )
            @Html.ValidationMessageFor(model => model.CreationDate)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
        </div>

    </fieldset>
}
        <div>
        @Html.ActionLink("See the List", "List")
        </div>
<div>
    @Html.ActionLink("Back to List", "About")
</div>
<script type="text/javascript">
    $(document).ready(function () {

        $("#RegionName").click(function () {
            $("#Rest").show();
        });
    });
</script>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Controller :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ExamplekendoDropdown.Models;

namespace ExamplekendoDropdown.Controllers
{
    public class FacilityGroupController : Controller
    {
        //
        // GET: /FacilityGroup/

        public ActionResult FacilityGroup()
        {
            return View();
        }
        [HttpPost]
        public ActionResult FacilityGroup(FacilityGroup objadd)
        {
            AMIEntities1 obj1 = new AMIEntities1();
            Facility objtbl = new Facility();

                objtbl.RegionId = Convert.ToInt16(objadd.RegionId);
                objtbl.FaclityGroupName = objadd.FaclityGroupName.ToString();
                objtbl.Status = objadd.status;
                objtbl.CreationDate = objadd.CreationDate;

                obj1.AddToFacilities(objtbl);
                obj1.SaveChanges();
                obj1.AcceptAllChanges();


            return View();
        }
    }
}

Kindly help !!

解决方案

I would suggest that you use DropDownListFor() and then also make sure that the .Name matches the name of the property. In your case:

 @(Html.Kendo().DropDownListFor(model => model.RegionId)
      .Name("RegionId")
      .DataTextField("RegionName")
      .DataValueField("RegionId")
      .DataSource(source =>
      {
          source.Read(read =>
          {
              read.Action("GetRegion", "Fill");
          });
      })
  )

这篇关于如何将剑道 Ui 下拉选择值的 ID 传递到控制器上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆