出于某种原因,当我按下视图中的任何按钮时,传递给action方法的模型的所有属性都为null:
查看:
@using (Html.BeginForm("FolderChange", "EdiSender", FormMethod.Post, new {id = "ediFilesForm"}))
{
var directoriesSelectList = new SelectList(Model.Directories);
@Html.DropDownListFor(m => m.SelectedDirectory, directoriesSelectList, new {@Id = "Directories", @style = "width:Auto;", @size = 20, onchange = "$('#ediFilesForm').submit()", name = "action:FolderChange"})
var ediFilesSelectList = new SelectList(Model.EdiFileNames);
@Html.DropDownListFor(m => m.SelectedEdiFile, ediFilesSelectList, new {@Id = "EdiFileNames", @style = "width:Auto;", @size = 20})
}
<br/>
...
<form action="" method="post">
<input type="submit" value="Send" name="action:Send" />
<input type="submit" value="Delete" name="action:Delete" />
<input type="submit" value="Refresh" name="action:Refresh" />
</form>
控制器:
[HttpPost]
[MultipleButton(Name = "action", Argument = "Send")]
public ActionResult Send(EdiFileModel ediFileModel)
{
....
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MultipleButtonAttribute : ActionNameSelectorAttribute
{
public string Name { get; set; }
public string Argument { get; set; }
public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
{
var isValidName = false;
var keyValue = string.Format("{0}:{1}", Name, Argument);
var value = controllerContext.Controller.ValueProvider.GetValue(keyValue);
if (value != null)
{
controllerContext.Controller.ControllerContext.RouteData.Values[Name] = Argument;
isValidName = true;
}
return isValidName;
}
}
当我的按钮位于Html.BeginForm()块中时,它曾经工作,但我不能再这样了,因为它现在具有动作名称FolderChange(),它与例如发送()或处理按钮按下的其他操作方法。
感谢。
编辑:
@section scripts
{
<sctipt type="text/javascript">
$("#Directories").change(function () {
var selectedDirectory = $("#Directories").val();
$(function () {
$.getJSON('/DoWork/FolderChangeAjaxCall?selectedDirectory=' + selectedDirectory, function (result) {
var ddl = $('#EdiFileNames');
ddl.empty();
$(result).each(function () {
$(document.createElement('option'))
.attr('value', this.Id)
.text(this.Value)
.appendTo(ddl);
});
});
});
});
</sctipt>
}
<h2>Existing EDI Files</h2>
@using (Html.BeginForm("FolderChange", "EdiSender", FormMethod.Post, new {id = "ediFilesForm"}))
{
var directoriesSelectList = new SelectList(Model.Directories);
@Html.DropDownListFor(m => m.SelectedDirectory, directoriesSelectList, new {@Id = "Directories", @style = "width:Auto;", @size = 20})
var ediFilesSelectList = new SelectList(Model.EdiFileNames);
@Html.DropDownListFor(m => m.SelectedEdiFile, ediFilesSelectList, new {@Id = "EdiFileNames", @style = "width:Auto;", @size = 20})
答案 0 :(得分:1)
在这里,它可能远离你认为的答案,但它将级联下拉列表。你将不得不替换你的价值观和背景,但它会起作用。现在,您可以将按钮放在表单中,而不必让下拉列表提交表单,这是相当糟糕的设计。
@Html.DropDownListFor(x => x.ddlID, Model.myDDLList, new { id = "ddl" })
@Html.DropDownListFor(x => x.myDDLList2, Model.myDDLList2, new { id = "foo" })
脚本
$("#ddl").change(function () {
var Id = $("#ddl").val(); //1st dropdown Value
$(function () {
$.getJSON('/DoWork/AjaxCall?Id=' + Id, function (result) {
var ddl = $('#foo');
ddl.empty();
$(result).each(function () {
$(document.createElement('option'))
.attr('value', this.Field1)
.text(this.Field2)
.appendTo(ddl);
});
});
});
});
控制器
public ActionResult AjaxCall(int Id)
{
using (PerformanceEntities context = new PerformanceEntities())
{
return this.Json(
(from obj in context.ACCOUNT.Where(x => x.ACCT_ID == Id).ToList()
select new
{
Field1 = obj.ACCT_ID,
Field2 = obj.ACCT_NAME
}),
JsonRequestBehavior.AllowGet
);
}
}
您可以查看this帖子以供参考