Asp.net mvc post方法期间从局部视图检索值
我有一个包含下拉列表的视图,在选中的下拉列表项上,我加载一个局部视图。当表单提交时,我希望能够在表单提交期间从主视图和部分视图中获取值。 这是主视图Asp.net mvc post方法期间从局部视图检索值,asp.net-mvc,forms,asp.net-mvc-4,partial-views,Asp.net Mvc,Forms,Asp.net Mvc 4,Partial Views,我有一个包含下拉列表的视图,在选中的下拉列表项上,我加载一个局部视图。当表单提交时,我希望能够在表单提交期间从主视图和部分视图中获取值。 这是主视图 @model AdminPortal.Areas.Hardware.Models.CreateModule @{ ViewBag.Title = "Create Module"; Layout = "~/Views/shared/_BootstrapLayout.basic.cshtml"; } @Html.ValidationS
@model AdminPortal.Areas.Hardware.Models.CreateModule
@{
ViewBag.Title = "Create Module";
Layout = "~/Views/shared/_BootstrapLayout.basic.cshtml";
}
@Html.ValidationSummary(true)
<fieldset class="form-horizontal">
<legend>Add a Module <small>Create</small></legend>
@using (Html.BeginForm("CreateModule", "Module", new{id="AddModuleForm"}))
{
@Html.ValidationSummary(true)
<div class ="controls">
<div class="input-block-level">@Html.TextBoxFor(model => model.ModuleId, new {@placeholder = "ModuleID"})</div>
<br/>
<div class ="input-block-level" id="selectedModuleTypeName">@Html.DropDownListFor(model => model.SelectedModuleTypeName, Model.TypeNames,"Select Moduletype", new{id = "ModuleList"})</div>
<br/>
<div id="partialDiv"></div>
</div>
<div class="form-actions" id="buttons">
<button type="submit" class="btn btn-primary" id="Submit">Save changes</button>
@Html.ActionLink("Cancel", "ModuleList", null, new { @class = "btn " })
</div>
}
</fieldset>
<div>
@Html.ActionLink("Back to List", "ModuleList")
</div>
<script>
$("#buttons").hide();
$("#ModuleList").on("change", function() {
var modId = $(this).val();
$.get('@Url.Action("GetModulePropertyName", "Module")', { moduleTypeValue: modId }, function(result) {
$("#partialDiv").html(result);
});
//uncomment following section to check if the partial view is working properly
/*.done(function() { alert("done"); })
.fail(function() { alert("fail"); })
.always(function() { alert("completed"); });*/
});
$("#buttons").show();
</script>
@model IEnumerable<string>
@foreach(var names in Model)
{
<div class="input-block-level">@Html.TextBoxFor(m=>names, new{Value="", placeholder=names})</div>
<br/>
}
[HttpPost]
public ActionResult CreateModule(CreateModule moduleV)
{
var module = new Module
{
ModuleTypeId = Convert.ToInt64(moduleV.SelectedModuleTypeName),
ModuleId = moduleV.ModuleId,
DateEntered = moduleV.DateEntered,
};
if (ModelState.IsValid)
{
_repository.AddModule(module);
Success("Module added successfully!");
return RedirectToAction("ModuleList", "Module", new {area = "Hardware"});
}
Error("Something went wrong!");
return RedirectToAction("CreateModule", "Module", new { area = "Hardware" });
}
最后,这里是主视图的httppost方法
@model AdminPortal.Areas.Hardware.Models.CreateModule
@{
ViewBag.Title = "Create Module";
Layout = "~/Views/shared/_BootstrapLayout.basic.cshtml";
}
@Html.ValidationSummary(true)
<fieldset class="form-horizontal">
<legend>Add a Module <small>Create</small></legend>
@using (Html.BeginForm("CreateModule", "Module", new{id="AddModuleForm"}))
{
@Html.ValidationSummary(true)
<div class ="controls">
<div class="input-block-level">@Html.TextBoxFor(model => model.ModuleId, new {@placeholder = "ModuleID"})</div>
<br/>
<div class ="input-block-level" id="selectedModuleTypeName">@Html.DropDownListFor(model => model.SelectedModuleTypeName, Model.TypeNames,"Select Moduletype", new{id = "ModuleList"})</div>
<br/>
<div id="partialDiv"></div>
</div>
<div class="form-actions" id="buttons">
<button type="submit" class="btn btn-primary" id="Submit">Save changes</button>
@Html.ActionLink("Cancel", "ModuleList", null, new { @class = "btn " })
</div>
}
</fieldset>
<div>
@Html.ActionLink("Back to List", "ModuleList")
</div>
<script>
$("#buttons").hide();
$("#ModuleList").on("change", function() {
var modId = $(this).val();
$.get('@Url.Action("GetModulePropertyName", "Module")', { moduleTypeValue: modId }, function(result) {
$("#partialDiv").html(result);
});
//uncomment following section to check if the partial view is working properly
/*.done(function() { alert("done"); })
.fail(function() { alert("fail"); })
.always(function() { alert("completed"); });*/
});
$("#buttons").show();
</script>
@model IEnumerable<string>
@foreach(var names in Model)
{
<div class="input-block-level">@Html.TextBoxFor(m=>names, new{Value="", placeholder=names})</div>
<br/>
}
[HttpPost]
public ActionResult CreateModule(CreateModule moduleV)
{
var module = new Module
{
ModuleTypeId = Convert.ToInt64(moduleV.SelectedModuleTypeName),
ModuleId = moduleV.ModuleId,
DateEntered = moduleV.DateEntered,
};
if (ModelState.IsValid)
{
_repository.AddModule(module);
Success("Module added successfully!");
return RedirectToAction("ModuleList", "Module", new {area = "Hardware"});
}
Error("Something went wrong!");
return RedirectToAction("CreateModule", "Module", new { area = "Hardware" });
}
当前情况:
发布表单时,通过部分视图传递的模型的属性值为null。我得到其他值,比如typename、模块ID
我想要什么:
我还想获取通过部分视图传递的属性的值 尝试使用
List<Property>
列表
作为局部视图中的模型,并将CreateModule.Properties作为模型从视图中传递问题是模型绑定器无法在那里找到答案 @TextBoxFor(m=>names,new{Value=”“,placeholder=names}) 属于,因为“名称”不是模型类上的属性。如果需要绑定到CreateModule.Properties,则需要更改局部视图以生成具有适当名称的文本框,如以下内容:
@model IEnumerable<string>
@
{
int i=0;
}
@foreach(var names in Model)
{
<div class="input-block-level">@Html.TextBox("Properties[" + i + "].Value")</div>
<br/>
}
@model IEnumerable
@
{
int i=0;
}
@foreach(模型中的变量名称)
{
@文本框(“属性[“+i+”].Value”)
}
表单中的任何位置都没有用于属性的任何输入字段。所以它总是空的。这很正常
以下是您可以继续操作的方法。首先设置正确的导航属性,以便帮助器生成相应输入字段的正确名称
如果您希望能够正确地获取零件,还应确保将IEnumerable
模型传递给零件:
[HttpGet]
public ActionResult GetModulePropertyName(string moduleTypeValue)
{
var moduleKindId = _repository.GetModuleKindId(moduleTypeValue);
IList<Property> model = ...
return PartialView("GetModulePropertyName", model.ToList());
}
谢谢,我也这么想。但是在我的HttpGet
方法中,主视图中的脚本将转发到该方法。我怎样才能正确地改变我的方法?我可以在那里做些什么,以便将值传递给我的部分视图,并使用您所说的视图模型?我仍然会为属性获取null值:(这是我在控制器中为[httpget]public ActionResult GetModulePropertyName(string moduleTypeValue){var-temp=\u repository.GetModuleKindPropertyNames所做的事情(moduleTypeValue);IList model=temp.Select(item=>新属性{Name=item}).ToList();返回PartialView(“GetModulePropertyName”,model);}
视图与您提到的完全一样。您能粘贴生成的一些隐藏输入字段的名称吗?您是指由部分视图生成的隐藏输入字段名称吗?如果是,那么我运行开发人员工具并检查元素,这里是为部分视图
和生成的e>
我明白了,名称
有问题。它应该是属性[0]。名称
而不是属性[0]。名称
。我将看到如何修复该问题,并在稍后更新我的答案。
@model Property
<div class="input-block-level">
@Html.HiddenFor(m => m.Name)
@Html.TextBoxFor(m => m.Value, new { placeholder = Model.Name })
</div>
<br />