Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在mvc中将数据从视图传递到控制器_C#_Asp.net Mvc - Fatal编程技术网

C# 在mvc中将数据从视图传递到控制器

C# 在mvc中将数据从视图传递到控制器,c#,asp.net-mvc,C#,Asp.net Mvc,鉴于我有DropDownList,当选择某个项目时,从控制器调用功能 我试图将参数传递到视图中的控制器中 我所拥有的是,我可以从index.html调用Homecontroller.cs中的函数,而无需参数,现在我所需要的只是传递一些字符串 我现在的代码: **Index.cshtml:** @using (Ajax.BeginForm("nMap", "Home", new AjaxOptions { HttpMethod = "Get",

鉴于我有DropDownList,当选择某个项目时,从控制器调用功能

我试图将参数传递到视图中的控制器中

我所拥有的是,我可以从index.html调用Homecontroller.cs中的函数,而无需参数,现在我所需要的只是传递一些字符串

我现在的代码:

    **Index.cshtml:**

@using (Ajax.BeginForm("nMap", "Home", new AjaxOptions
{
    HttpMethod = "Get",                                              
    InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace
}))
{
   @Html.DropDownListFor(x => x.SelectedFileName, Model.Files, new { Name = "map", @class = "form-control", onchange = "CallChangefunc()" })
}     
.
.
.
<script type="text/javascript">
   function CallChangefunc() {
    window.location.href = '@Url.Action("nMap", "Home")';
   }
</script>
我所需要的是:

Index.cshtml:

 @Html.DropDownListFor(x => x.SelectedFileName, Model.Files, new { Name = "map", @class = "form-control", onchange = "CallChangefunc("+someStringParam+")" })
Homecontroller.cs

[ActionName("nMap")]
public ActionResult NMap(string someStringParam)
{
 //do something with the param
}
我如何做到这一点?

改变

[ActionName("nMap")]
public ActionResult NMap(string someStringParam)
{
 //do something with the param
}

MVC使用命名约定来排列参数映射

改变

@Html.DropDownListFor(x => x.SelectedFileName, Model.Files, new { Name     = "map", @class = "form-control", onchange = "CallChangefunc()" }

并且完全摆脱JavaScript

public class HomeVM
{
    public List<SelectListItem> Files { get; set; }
    public string SelectedFileName { get; internal set; }
    public List<string> DynamicAlgorithems { get; set; }
}
公共类HomeVM
{
公共列表文件{get;set;}
公共字符串SelectedFileName{get;internal set;}
公共列表动态算法{get;set;}
}
应该是

public class HomeVM
{
    public List<SelectListItem> Files { get; set; }
    public SelectListItem SelectedFileName { get; internal set; }
    public List<string> DynamicAlgorithems { get; set; }
}
公共类HomeVM
{
公共列表文件{get;set;}
public SelectListItem SelectedFileName{get;internal set;}
公共列表动态算法{get;set;}
}

当我选择某个项目时,它会调用函数,但它说,
SelectedFileName
为空,你知道为什么吗?很抱歉,我错过了VM。文件不是字符串类型。请参阅更新。您还可以将方法参数更改为HomeVM类型,并从VM内部获取SelectedListItem,但在此处不需要。SelectedFileName.Value为空字符串,您知道原因吗?
@Html.DropDownListFor(x => x.SelectedFileName, Model.Files, new { Name     = "map", @class = "form-control", onchange = "CallChangefunc()" }
@Html.DropDownListFor(x => x.SelectedFileName, Model.Files, new { Name     = "map", @class = "form-control", @onchange = "this.form.submit();"})
public class HomeVM
{
    public List<SelectListItem> Files { get; set; }
    public string SelectedFileName { get; internal set; }
    public List<string> DynamicAlgorithems { get; set; }
}
public class HomeVM
{
    public List<SelectListItem> Files { get; set; }
    public SelectListItem SelectedFileName { get; internal set; }
    public List<string> DynamicAlgorithems { get; set; }
}