【问题标题】:Passing Value from Kendo DDL to ActionLink/ActionImage without submitting values将值从 Kendo DDL 传递到 ActionLink/ActionImage 而不提交值
【发布时间】:2013-08-31 23:41:33
【问题描述】:

VS'12 KendoUI InternetApplication Template C# asp.net EF Code First

我有 2 个下拉列表页面 1 个页面使用 DropDownList 将值发布到 ModelView 另一个只是通过 ID 返回。

疑问

基本上,我想通过用户可以选择的 DDL 留在视图中。此 DDL 的左侧有一个 Actionlink,应该能够使用所选参数转到新的 ACtion。但我的模型值始终为空。如何传递这个值?

下拉列表

                <label for="Prospects">Prospect:</label>
                @(Html.Kendo().DropDownListFor(m=>m.Prospects)
                      //.Name("clients")
                      .HtmlAttributes(new { style = "width:300px"}) //, id = "clients"})
                      .OptionLabel("Select Prospect...")
                      .DataTextField("ProspectName")
                      .DataValueField("ProspectID")
                      .DataSource(source => {
                           source.Read(read => {
                               read.Action("GetCascadeProspects", "AddCCCTRST");
                           });
                      })
                  )

ActionLink

@Html.ActionImage("ADD", "Prospect", new { id=Model.Prospects.FirstOrDefault() } , "~/Images/Navigation/Add.PNG", "Add")

..., new { id=Model.Prospects.FirstOrDefault() } ,...,...) 
// the Model is null here, how can i pass my value to this actionlink / actionimage?

(ActionImage 来自This Post) 如果代码需要贴在这里请指教。 (自定义 ActionImage 正在工作,我只是无法传递值....)

更新模型

public class ViewModelCCTRST
{
    public string Clients { get; set; }
    public IEnumerable<dbClient> AvailableClients { get; set; }
    public string Prospects { get; set; }
    public IEnumerable<dbProspect> AvailableProspects { get; set; }
    public string Countys { get; set; }
    public IEnumerable<dbCounty> AvailableCounties { get; set; }
    public string TownShips { get; set; }
    public IEnumerable<dbTownShip> AvailableTownShip { get; set; }
    public string Ranges { get; set; }
    public IEnumerable<dbRange> AvailableRanges { get; set; }
    public string Sections { get; set; }
    public IEnumerable<dbSection> AvailableSection { get; set; }
    public string Tracts { get; set; }
    public IEnumerable<dbTract> AvailableTracts { get; set; }
}

Controller *A JsonResult GET 方法和 Post 请求

    public JsonResult GetCascadeTracts(int sections)
    {
        var tract = db.Tracts.AsQueryable();

        if (0 != sections)
        {
            tract = tract.Where(o => o.SectionID == sections);
        }

        return Json(tract.Select(o => new { TractID = o.TractID, TractName = o.TractNumber }),    JsonRequestBehavior.AllowGet);
    }

    [HttpPost]
    public ActionResult Index(ViewModelCCTRST model)
    {

        if (ModelState.IsValid)
        {

            //string clt = model.Clients;
            string pro = model.Prospects;
            string cnt = model.Countys;
            string twn = model.TownShips;
            string rng = model.Ranges;
            string sct = model.Sections;
            string trt = model.Tracts;

            return Content(cnt + twn + rng + sct + trt);
        }
        else
        {
            return Content("");
        }
    }

**正如下面讨论的,我的 Post 方法中确实有值,但我的 View 中没有任何值(这是我希望能够有值来使用我的 Actionlinks w /o 刷新页面)另外,如果我将Model.(w/e the data is) 绑定到action links 他们会给我一个null reference

【问题讨论】:

    标签: asp.net-mvc razor kendo-ui actionlink


    【解决方案1】:

    您可以为此使用kendo template参考:Kendo DropDownList / Customizing templates

    @(Html.Kendo().DropDownList()
          .Name("customers")
          .HtmlAttributes(new { style = "width: 400px" })
          .DataTextField("ContactName")
          .DataValueField("CustomerID")
          .DataSource(source =>
          {
              source.Read(read =>
              {
                  read.Action("GetCustomers", "Home");
              });
          })
          .Height(300)
          .Template("<img src=\"" + Url.Content("~/Content/web/Customers/") +  "${data.CustomerID}.jpg\" alt=\"${data.CustomerID}\" />" +
                            "<dl>" +
                                "<dt>Contact:</dt><dd>${ data.ContactName }</dd>" +
                                "<dt>Company:</dt><dd>${ data.CompanyName }</dd>" +
                            "</dl>")
    )
    

    更新

    您得到空值是因为您没有将Name 分配给您的剑道控件。如果不这样做,控件值将不会绑定到您的模型。

    @(Html.Kendo().DropDownListFor(m=>m.Prospects)
                      .Name("Prospects")         // assign appropriate name
                      .HtmlAttributes(new { style = "width:300px"}) //, id = "clients"})
                      .OptionLabel("Select Prospect...")
                      .DataTextField("ProspectName")
                      .DataValueField("ProspectID")
                      .DataSource(source => {
                           source.Read(read => {
                               read.Action("GetCascadeProspects", "AddCCCTRST");
                           });
                      })
                  )
    

    【讨论】:

    • Error 1 The best overloaded method match for 'Kendo.Mvc.UI.Fluent.DropDownListBuilderBase&lt;Kendo.Mvc.UI.DropDownList,Kendo.Mvc.UI.Fluent.DropDownListBuilder&gt;.Template(string)' has some invalid argument Error 2 Argument 1: cannot convert from 'System.Web.Mvc.MvcHtmlString' to 'string' 运行第二个答案样本时。
    • 我认为您面临的错误是因为Template 的参数中没有模板字符串。您必须将数据作为Add 操作方法的参数传递。我不知道你的逻辑是什么,但我可以告诉你,你面临的错误是 @Url.Action(\"Add\", new { id = Model.Prospects.FirstOrDefault()}) 因此。参考我为剑道提供的链接,它一定会对你有所帮助..
    • 我认为我们的沟通有误,我想弄清楚为什么在@Url.Action(\"Add\", new { id = Model.Prospects.FirstOrDefault()} 这部分new { id = Model.Prospects.FirstOrDefault() 没有价值。这就是我在提交后从我的控制器访问数据的方式,所以它可能不存在,因为没有提交? - 在那种情况下,问题仍然存在,如何从 DDL 中获取选定的值到我的操作链接。值为[Null]
    • @DonThomasBoyle:请分享控制器 ActionMethod 的代码。你期望通过什么。另外,显示模型类,以便我了解它..
    • @DonThomasBoyle:我已经更新了答案。您需要正确分配Name 属性...请参阅:how to pass the ID of the kendo Ui dropdown selected value onto the controller?
    【解决方案2】:
    1. 将 id 添加到 Actionlink

      @Html.ActionLink("Add", "Create", new { controller = "Client"}, new {id="AddClient"}) 
      
    2. 添加以捕获 onclick 事件

       $('#AddClient').click(function (e) {
          var val = $("#Clients").val();
          var href = "/Client/Create/" + val;
          this.href = ""; //clears out old href for reuse
          this.href = href; //changes href value to currently slected dropdown value
      });
      

    srcs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-12
      • 2012-01-16
      • 2020-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多