我有一個視圖,其中包含下拉列表,並且在選擇的下拉列表項目中加載了部分視圖。當提交表單時,我希望能夠在表單提交時從主視圖和部分視圖中獲取值。 這裏是主視圖在post方法中從部分視圖中檢索值
@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/>
}
這裏是我的模式
public class CreateModule
{
//Empty form to handle form serialization
public CreateModule()
{
}
[Required]
public string ModuleId { get; set; }
[DataType(DataType.DateTime)]
public DateTime DateEntered { get; set; }
[Required]
public string SelectedModuleTypeName { get; set; }
public IEnumerable<SelectListItem> TypeNames { get; set; }
public List<Property> Properties { get; set; }
}
public class Property
{
public string Name { get; set; }
public string Value { get; set; }
}
這裏是方法那腳本在主視圖中向前到
[HttpGet]
public ActionResult GetModulePropertyName(string moduleTypeValue)
{
var moduleKindId = _repository.GetModuleKindId(moduleTypeValue);
var modulePropertyNames = _repository.GetModuleKindPropertyNames(moduleTypeValue);
return PartialView("GetModulePropertyName",modulePropertyNames);
}
,並終於在這裏是主視圖httppost方法
[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。
我想要什麼: 我也想獲得通過局部視圖傳遞的屬性的值。
我仍然得到了性能空值:(這是我在控制器已經做了[HTTPGET]'公衆的ActionResult GetModulePropertyName(字符串moduleTypeValue) {VAR 溫度= _repository.GetModuleKindPropertyNames(moduleTypeValue); 的IList模型=溫度.Select(item => new Property {Name = item})。ToList(); return PartialView(「GetModulePropertyName」,model); }'和視圖與您所提到的一樣 –
Cybercop
您可以粘貼'name ' –
你的意思是由部分視圖生成的隱藏輸入字段名稱? – Cybercop