我正在编写一个MVC3项目,该项目显示创建事件时可供选择的用户主题。但是,每当从下拉列表中选择一个值时,字段“Topic”的值始终为空。
这是班级:
public class VolunteerEvent
{
public int VolunteerEventID { get; set; }
public DateTime VolunteerDate { get; set; }
public int ZIPcode { get; set; }
public int Hours { get; set; }
public string Topic { get; set; }
}
存储字符串的虚拟类
public class VolunteerTopic
{
public int VolunteerTopicID { get; set; }
public string VolunteerTopicName { get; set; }
}
创建操作的控制器
public ActionResult Create()
{
ViewBag.VolunteerTopicID = new SelectList(db.VolunteerTopics, "VolunteerTopicID", "VolunteerTopicName");
return View();
}
//
// POST: /LogVolunteerEvent/Create
[HttpPost]
public ActionResult Create(VolunteerEvent volunteerevent)
{
if (ModelState.IsValid)
{
db.VolunteerEvents.Add(volunteerevent);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.VolunteerTopics = new SelectList(db.VolunteerTopics, "VolunteerTopicID", "VolunteerTopicName",
volunteerevent.Topic);
return View(volunteerevent);
}
以及视图的相关部分
<div class="editor-label">
@Html.LabelFor(model => model.Topic)
</div>
<div class="editor-field">
@Html.DropDownList("VolunteerTopicID")
@Html.ValidationMessageFor(model => model.Topic)
</div>
感兴趣的人的HTML结果
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Create</title>
<link href="/Content/Site.css" rel="stylesheet" type="text/css" />
<script src="/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="/Scripts/modernizr-2.5.3.js" type="text/javascript"></script>
</head>
<body>
<div class="page">
<h2>Create</h2>
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<form action="/logvolunteerEvent/Create" method="post"> <fieldset>
<legend>VolunteerEvent</legend>
<div class="editor-label">
<label for="VolunteerDate">VolunteerDate</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-required="The VolunteerDate field is required." id="VolunteerDate" name="VolunteerDate" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="VolunteerDate" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="ZIPcode">ZIPcode</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-number="The field ZIPcode must be a number." data-val-required="The ZIPcode field is required." id="ZIPcode" name="ZIPcode" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="ZIPcode" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="Hours">Hours</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-number="The field Hours must be a number." data-val-required="The Hours field is required." id="Hours" name="Hours" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="Hours" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="Topic">Topic</label>
</div>
<div class="editor-field">
<select id="VolunteerTopicID" name="VolunteerTopicID">
<option value="1"> Advocacy</option>
...a bunch of values...
</select>
<span class="field-validation-valid" data-valmsg-for="Topic" data-valmsg-replace="true"></span>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
</form>
<div>
<a href="/logvolunteerEvent">Back to List</a>
</div>
</section>
<footer>
</footer>
</div>
</body>
</html>
答案 0 :(得分:0)
我认为你弄错了,这就是你应该这样做的方式:
型号:
public class VolunteerEvent
{
// other fields
public int TopicId { get; set; }
}
查看:
@model VolunteerEvent
(inside some form)
@Html.DropDownListFor(m => m.TopicId, new SelectList(aListOfTopics, "Id", "Name"))
(这是Topic
个对象的列表,其列为Id
和Name
)
控制器:
[HttpPost]
public void Create(VolunteerEvent volunteerEvent, FormCollection collection)
{
var topicId = volunteerEvent.TopicId;
// do something
// you can also get the value from collection["TopicId"]
}
答案 1 :(得分:0)
此处的问题是您的Dropdown
列表绑定到VoluteerTopicId,
,但这不是您传递到帖子操作中的模型的一部分。
这里最快的解决方法是创建一个视图模型:
public class VolunteerViewModel
{
public VolunteerEvent VolunteerEvent {get; set;}
public VolunteerTopic {get; set;}
}
现在强烈地将视图输入到此视图模型而不是VolunteerEvent
并相应地重构。
然后将帖子操作的签名更改为public ActionResult Create(VolunteerViewModel viewModel)
现在,您的列表应绑定到模型,并且可以在viewModel.VolunteerTopic.VolunteerTopicId