简体   繁体   English

将下拉字符串从视图传递到控制器方法

[英]Passing a DropDown string from a View to a Controller method

My question is a two part question.我的问题是一个两部分的问题。

1) I am trying to pass a string from my Index view into a method ( UpdateView(string selectProductionLine) ) via an ajax call. 1)我试图通过ajax调用将一个字符串从我的索引视图传递到一个方法( UpdateView(string selectProductionLine) )中。 I can get the ajax call to call the method but I am unable to get the string to be anything but null.我可以通过 ajax 调用来调用该方法,但我无法将字符串变为空值。

2) After this method ( UpdateView(string selectProductionLine) ) is called, I want it to update the model and then call a partial view with that updated method. 2) UpdateView(string selectProductionLine)此方法( UpdateView(string selectProductionLine) )后,我希望它更新模型,然后使用该更新方法调用局部视图。 Currently I cannot get it to call that partial view.目前我无法让它调用该部分视图。

I have been looking at several different attempts at this, links below, and have been unable to get this to work.我一直在寻找几种不同的尝试,下面的链接,但一直无法让它发挥作用。 My JS is not all that great, I am still a beginner, and I am having trouble combining what others have done.我的 JS 并不是那么好,我还是一个初学者,我很难结合其他人所做的。

Index View:索引视图:

@(Html.Kendo().DropDownList()
      .Name("productionLine-dropdown")
      .DataTextField("Name")
      .DataValueField("Id")
      .DataSource(source =>
      {
           source.Read(read => { read.Action("GetDropDownList", "Home"); });
      })
      .Events(e =>
      {
           e.Close("OnClose");
      })
)
<div id="Dashboard">
@Html.Partial("~/Views/Home/_Home.cshtml", Model)
</div>

Java Script: Java脚本:

function OnClose() {
    var selectProductionLine = $("#productionLine-dropdown").data("kendoDropDownList").value();
    $("#Dashboard").load('/Home/UpdateView', selectProductionLine);
}

function DropDownValue() {
    var value = $("#productionLine-dropdown").data("kendoDropDownList").value();
    return { selectProductionLine: value };
}

Controller:控制器:

public ActionResult _Home(DisplayViewModel dvm)
{
    return PartialView(dvm);
}

public ActionResult UpdateView(string selectProductionLine)
{
    DisplayViewModel dvm = new DisplayViewModel();
    //Some logic
    return PartialView("~/Home/_Home.cshtml", dvm);
}

Questions:问题:

1) Pass a string from the Index into the UpdateView() method. 1) 将一个字符串从 Index 传递到UpdateView()方法中。

2) Call the _Home partial view from the UpdateView() with the new ViewModel. 2) 使用新的 ViewModel 从UpdateView() ) 调用_Home局部视图。

Thanks!谢谢!

Updating PartialView mvc 4 更新 PartialView mvc 4

Refreshing MVC PartialView with new Model on DropDownList Change 使用 DropDownList 更改上的新模型刷新 MVC PartialView

Passing a List/model from a View to a controller 将列表/模型从视图传递到控制器

https://cmatskas.com/update-an-mvc-partial-view-with-ajax/ https://cmatskas.com/update-an-mvc-partial-view-with-ajax/

Ok there is two issues here.好的,这里有两个问题。

Javascript Javascript

First, change your .Load method to simply concatenate the selecProductionLine value to the end of the URL as such.首先,更改您的 .Load 方法以简单地将 selecProductionLine 值连接到 URL 的末尾。

var selectProductionLine = $("#productionLine-dropdown").data("kendoDropDownList").value();
$("#Dashboard").load("/Home/UpdateView/" + selectProductionLine);

Controller Action控制器动作

Second, you will need to change the UpdateView function to take in Id like so.其次,您需要像这样更改 UpdateView 函数以接收 Id。

public ActionResult UpdateView(string Id)
{
    DisplayViewModel dvm = new DisplayViewModel();
    ProductionLine pl = _productionLineService.Find(Id);
    dvm.ProdLine = new ProductionLineViewModel
    {
        Id = pl.Id,
        CreatedAt = pl.CreatedAt,
        Name = pl.Name,
        ActiveLine = pl.ActiveLine,
        ComputerName = pl.ComputerName,
        UPE = pl.UPE
   };
   return PartialView("~/Home/_Home.cshtml", dvm);
}

This is because the default route in RouteConfig.cs uses {controller}/{action}/{id} as the default routing for any requests.这是因为 RouteConfig.cs 中的默认路由使用 {controller}/{action}/{id} 作为任何请求的默认路由。

RouteConfig路由配置

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM