我在创建用户的MVC4应用程序中创建了View
。在此视图中,用户可以为Organization
或Sponsor
设置属性,但不能同时设置两者。我当前的代码正确显示所有组织/赞助商,具体取决于根据Switch选择显示的内容,但当我在DropDownList
中进行选择并保存新用户时,所有DropDownLists返回的是{ {1}}的{1}}值。
用户模型(部分):
Null
创建(VIEW):
User
控制器(创建GET):
[GridColumn(Title = "Org.", SortEnabled = true, Width = "100")]
public int? MemberOrgId { get; set; }
[NotMappedColumn]
public int? SponsorOrgId { get; set; }
[ForeignKey("MemberOrgId")]
[NotMappedColumn]
public virtual MemberOrganizations Organization { get; set; }
[ForeignKey("SponsorOrgId")]
[NotMappedColumn]
public virtual SponsorOrganizations Sponsor { get; set; }
控制器(创建HTTP-Post):
@model PROJECT.Models.Users
@{
ViewBag.Title = "Create";
Layout = "~/Areas/Admin/.../.../.../_AdminLayout.cshtml";
string cancelEditUrl = "/Admin/UserController/";
}
@using (Html.BeginForm("Create", "UserController", FormMethod.Post, new { enctype = "multipart/form-data" })) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.RegisteredDate)
<div class="container">
<div class="row">
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field" style="margin-bottom: 15px">
@Html.TextBoxFor(model => model.Email, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Email)
</div>
</div>
<input type="checkbox" value="12345" name="Sponsor-Organization" checked class="userCreate-BSSwitch"/>
<div style="margin-bottom: 15px">
<div class="row switchOn">
<div class="editor-label">
@Html.LabelFor(model => model.MemberOrgId, "Organization")
</div>
<div class="editor-field">
@Html.DropDownList("OrganizationId", null, String.Empty, new { @class = "form-control", @id = "OrgIdDropDown" })
@Html.ValidationMessageFor(model => model.MemberOrgId)
</div>
</div>
<div class="row switchOff">
<dliv class="editor-label">
@Html.LabelFor(model => model.SponsorOrgId, "Sponsor")
</dliv>
<div class="editor-field" >
@Html.DropDownList("SponsorId", null, String.Empty, new { @class = "form-control", @id = "SponsorIdDropDown" })
@Html.ValidationMessageFor(model => model.SponsorOrgId)
</div>
</div>
</div>
<div class="row" id="submitRow">
<div class="btn-group ">
<button type="submit" value="Save" class="btn btn-success">Create User</button>
</div>
<a href="@cancelEditUrl" onclick="confirmCancel()" class="btn btn-danger">Cancel</a>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
<script type="text/javascript">
jQuery(document).ready(function () {
setTimeout(function () { $("#alert").alert('close'); }, 5000);
$('.switchOff').addClass('hide');
});
$.fn.bootstrapSwitch.defaults.onText = 'Member';
$.fn.bootstrapSwitch.defaults.offText = 'Sponsor';
$.fn.bootstrapSwitch.defaults.offColor = 'info';
$.fn.bootstrapSwitch.defaults.animate = false;
//$.fn.bootstrapSwitch.defaults.size = 'large';
$(document).ready(function () {
$('input:checkbox[name="Sponsor-Organization"]').bootstrapSwitch();
});
$('input:checkbox[name="Sponsor-Organization"]').on('switchChange.bootstrapSwitch', function (event, state) {
var checked = state;
if (checked) {
$('.switchOn').removeClass('hide');
$('.switchOff').addClass('hide');
$('#SponsorIdDropDown').val("");
}
else {
$('.switchOff').removeClass('hide');
$('.switchOn').addClass('hide');
$('#OrgIdDropDown').val("");
}
});
$(document).ready(function () {
$(".btn-danger").click(function () {
var cancel = confirm("Are you sure? Entered data will be lost.")
if (cancel != true) {
event.preventDefault(); // cancel the event
}
});
});
//$('input:checkbox[name="Sponsor-Organization"]').on('switchChange.bootstrapSwitch', function(event, state) {
</script>
有人对此事有所了解吗?我在其他问题中尝试了一些不同的建议,但到目前为止还没有任何效果。这是我的第一个MVC应用程序,所以我觉得我可能会忽略一些非常基本的东西。
答案 0 :(得分:1)
对于要绑定到模型的字段,它必须位于&#34; for&#34;助手(显示除外)。尝试像这样改变你的下拉
@Html.DropDownListFor(x => x.OrganizationId, null, String.Empty, new { @class = "form-control"})
@Html.DropDownListFor(x => x.SponsorId, null, String.Empty, new { @class = "form-control" })
假设您的用户模型具有字段OrganizationId和SponsorId,那些字段将与下拉列表相关联(将它们设置为get并且将设置下拉列表并且下拉值将通过帖子传递回控制器)
修改
我建议您通过模型传递下拉列表。到您的模型添加
public SelectList OrganizationList { get; set; }
public SelectList SponsorList { get; set; }
然后在你的控制器上(在获取中)
newUser.OranizationList = new SelectList(db.MemberOrganizations, "Id", "Name");
newUser.SponsorList = new SelectList(db.SponsorOrganizations, "Id", "Name");
然后在你的观点
@Html.DropDownListFor(x => x.MemberOrgId, Model.OrganizationList, new { @class = "form-control" })
答案 1 :(得分:0)
public class MyModel
{
public MyModel()
{
this.myDDLList = new List<SelectListItem>();
}
public List<SelectListItem> myDDLList { get; set; }
public int ddlID { get; set; }
}
public ActionResult Index()
{
MyModel model = new MyModel();
using (YourEntities context = new YourEntities())
{
var list = context.YourTable.ToList();
foreach (var item in list)
{
model.myDDLList.Add(new SelectListItem() { Text = item.NameField, Value = item.ValueField.ToString() });
}
}
return View(model);
}
@Html.DropDownListFor(x => x.ddlID, Model.myDDLList)
答案 2 :(得分:0)
我通过使用ViewData[]
将预先填充的选择列表从我的控制器传递到我的视图来解决了这个问题:
// GET: /Admin/
public ActionResult Create()
{
ViewBag.headerTitle = "Create a User";
ViewData["Organization"] = new SelectList(db.MemberOrganizations, "Id", "Name");
ViewData["Sponsor"] = new SelectList(db.SponsorOrganizations, "Id", "Name");
Users newUser = new Users();
newUser.RegisteredDate = DateTime.Now;
newUser.LastVisitDate = DateTime.Now;
newUser.ProfilePictureSrc = null;
return View(newUser);
}
然后在我看来,只需将ViewData[]
值作为单独的Html.DropDownList
读入我的SelectList
:
@Html.DropDownList("MemberOrgId", ViewData["Organization"] as SelectList, String.Empty, new { @class = "form-control", @id = "MemberOrgId" })
@Html.DropDownList("SponsorOrgId", ViewData["Sponsor"] as SelectList, String.Empty, new { @class = "form-control", @id = "SponsorOrgId" })