我正在编写一些javascript,其中下拉菜单中的所选选项被分配给hiddenfor值。这个hiddenfor使用了一个模型属性(SelectedModule)。
单击提交按钮时,即使我使用javascript为其指定了值,model.SelectedModule仍具有空值。
查看
@model UserManager.Models.vw_UserManager_Model
@{
ViewBag.Title = "EditUser";
}
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div id="edit-user">
<fieldset>
<legend>Edit user details</legend>
<div class="editor-field">
@Html.DropDownListFor(Model => Model.salutation, new List<SelectListItem>
{
new SelectListItem{ Text="Mr", Value = "Mr" },
new SelectListItem{ Text="Mrs", Value = "Mrs" },
new SelectListItem{ Text="Miss", Value = "Miss" },
new SelectListItem{ Text="Ms", Value = "Ms" },
new SelectListItem{ Text="Dr", Value = "Dr" }
})
@Html.ValidationMessageFor(model => Model.salutation)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.firstname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.firstname)
@Html.ValidationMessageFor(model => model.firstname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.lastname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.lastname)
@Html.ValidationMessageFor(model => model.lastname)
</div>
@{Html.Partial("~/Views/Partial/_AutocompleteGroupName.cshtml", this.ViewData);}
<div class="editor-label">
@Html.LabelFor(model => model.isactive)
</div>
<div class="editor-field">
@Html.EditorFor(model => Model.isactive)
@Html.ValidationMessageFor(model => Model.isactive)
</div>
<div class="editor-label">
@Html.Label("Is approved")
</div>
<div class="editor-field">
@Html.EditorFor(model => Model.IsApproved)
@Html.ValidationMessageFor(model => Model.IsApproved)
</div>
<div class="editor-label">
@Html.Label("Maximum concurrent users")
</div>
<div class="editor-field">
@Html.EditorFor(model => Model.MaxConcurrentUsers)
@Html.ValidationMessageFor(model => Model.MaxConcurrentUsers)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.email)
@Html.ValidationMessageFor(model => model.email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.rowtype)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.rowtype, new { @readonly = "readonly", @id = "txtNonEditableRowType" })
- Non editable
</div>
<div class="editor-label">
@Html.Label("Current Module")
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.module_name, new { @readonly = "readonly", @id = "txtNonEditableModule" })
- Non editable
@Html.ValidationMessageFor(model => model.module_name)
</div>
<br />
@if (Model.rowtype == "ALF")
{
<div id="alfModules">
@Html.Label("New module")
<br />
@{Html.RenderAction("_CreateUserModulesAlf", "UserManager");}
</div>
}
@if (Model.rowtype == "BRAD")
{
<div id="bradModules">
@Html.Label("New module")
<br />
@{Html.RenderAction("_CreateUserModulesBrad", "UserManager");}
</div>
}
<div class="editor-label">
@Html.LabelFor(model => model.group_name)
</div>
@* <div class="editor-field">
@Html.EditorFor(model => model.group_name)
@Html.ValidationMessageFor(model => model.group_name)
</div>*@
<div class="editor-label">
@Html.Label("Current Group")
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.group_name, new { @readonly = "readonly", @id = "txtNonEditableGroup" })
- Non editable
@Html.ValidationMessageFor(model => model.group_name)
</div>
@Html.HiddenFor(model => Model.selected_module, new { id = "hdnSelectedModule" })
@* @Html.HiddenFor(model => Model.selected_moduleAlf, new { id = "hdnSelectedModuleAlf" })
@Html.HiddenFor(model => Model.selected_moduleBrad, new { id = "hdnSelectedModuleBrad" })*@
<br />
<fieldset style="width: 400px; padding-left: 15px;">
<legend>Group Checker</legend>
<div id="createuser-groupnamesearch">
@{Html.RenderAction("_txtGroupSearchForm", "UserManager");}
</div>
</fieldset>
<p>
<input type="submit" value="Edit" onclick="newModule()" />
</p>
<br />
@Html.ActionLink("Back to User Manager Dashboard", "Index")
</fieldset>
</div>
}
<script type="text/javascript">
$("#group_name").autocomplete({
source: function (request, response) {
$.ajax({
url: '@Url.Action("LookUpGroupName", "UserManager")',
dataType: "json",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
value: request.term
},
success: function (data) {
response($.map(data, function (item) {
// alert(item.group);
return {
label: item.group,
value: item.group
} // end of return
})); // end of response
}, // end of success
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
} // end of error
}); // end of ajax
},
minLength: 2,
select: function (event, ui) { // Assign to hidden values to trigger onchange ajax call.
$.ajax({
url: '@Url.Action("GroupnameCheck", "UserManager")',
dataType: "json",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
value: ui.item.label
},
success: function (data) {
$.each(data, function (index, value) {
if (index == "AlfGroup") {
$("#txtGroupnameExistsAlf").val(value);
if ($("#txtGroupnameExistsAlf").val() == "Alf Group doesn't exist.") {
$("#txtGroupnameExistsAlf").css("background-color", "red");
}
else {
$('#txtGroupnameExistsAlf').css("background-color", "#33ff00");
}
}
if (index == "BradGroup") {
$("#txtGroupnameExistsBrad").val(value);
if ($("#txtGroupnameExistsBrad").val() == "Brad Group doesn't exist.") {
$("#txtGroupnameExistsBrad").css("background-color", "red");
}
else {
$('#txtGroupnameExistsBrad').css("background-color", "#33ff00");
}
}
});
}, // end of success
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
} // end of error
}); // end of ajax
$('#hdnGroupAlf').val(ui.item.label);
$('#hdnGroupBrad').val(ui.item.label);
},
open: function () {
$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
},
close: function () {
$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
}
});
function chkSelection() {
var rowType = $("#txtNonEditableRowType").val();
if (rowType == "ALF") {
var selectedVal = $("#ddlSelectedAlf option:selected").val();
$('#hdnSelectedModule').val(selectedVal);
}
else {
var selectedVal = $("#ddlSelectedBrad option:selected").val();
$('#hdnSelectedModule').text(selectedVal);
alert(selectedVal);
}
}
$(document).ready(function () {
// Non editable fields grey
$("#txtNonEditableGroup").css("background-color", "gray");
$("#txtNonEditableModule").css("background-color", "gray");
$("#txtNonEditableRowType").css("background-color", "gray");
// Show/Hide group check part based on IF ALF or BRAD
var rowType = $("#txtNonEditableRowType").val();
if (rowType == "ALF") {
$("#groupname-checker-alf").show();
$("#groupname-checker-brad").hide();
var selectedVal = $("txtNonEditableRowType").val();
$('#hdnModuleAlf').val(selectedVal);
}
else {
$("#groupname-checker-alf").hide();
$("#groupname-checker-brad").show();
var selectedVal = $("txtNonEditableRowType").val();
$('#hdnModuleBrad').val(selectedVal);
}
});
function newModule() { // Assign new selected module from dropdown to hidden form
// so it can be used in model as selected_module
if ($("#txtNonEditableRowType").val() == "ALF") {
var val = $("#module_name :selected").val();
$("#hdnSelectedModule").val(val);
}
else {
var val = $("#module_name :selected").val();
$("#hdnSelectedModule").val(val);
}
}
</script>
控制器
[HttpPost]
public ActionResult EditUser(vw_UserManager_Model model)
{
List<UserManager.Models.vw_UserManager_Model> modellist = new List<vw_UserManager_Model>();
int outcome = 0;
if (ModelState.IsValid)
{
outcome = UserManager.DAL.EditUser(model);
modellist.Add(model);
}
if (outcome == 1)
{
if (modellist.FirstOrDefault().rowtype == "Alf")
{
}
else
{
}
return RedirectToAction("showSuccessUser", new
{
CrudType = "Edit",
UserName = modellist.FirstOrDefault().UserName,
Password = modellist.FirstOrDefault().password,
FirstName = modellist.FirstOrDefault().firstname,
LastName = modellist.FirstOrDefault().lastname,
Email = modellist.FirstOrDefault().email,
GroupName = modellist.FirstOrDefault().group_name,
IsActive = modellist.FirstOrDefault().isactive,
selected_module = modellist.FirstOrDefault().module_name
});
}
else
{
ViewBag.Message = "Failure";
return RedirectToAction("showError", model);
}
}
摘要
在下拉菜单中选择项目时,该值将分配给HiddenFor字段。这可以通过使用firebug我可以看到值。
问题是当我提交表单时,我的C#代码中的model.SelectedModule属性具有空值。
有谁知道为什么?
答案 0 :(得分:1)
我不知道这是不是问题,但在你的一个案例中你是这样做的:
$('#hdnSelectedModule').text(selectedVal);
应该是:
$('#hdnSelectedModule').val(selectedVal);
此外,由于您已经在使用FireBug,请检查Net
选项卡中的AJAX请求。您将看到发送到服务器的所有键/值对,并且能够更轻松地识别问题。例如,请求中是否有SelectedModule=some_value
?如果是这样,假设您的模型具有名为SelectedModule
的属性并且属于简单类型(例如字符串),您将能够获得其值。