我正在尝试使用create函数将用户选择的值输入数据库。按下创建按钮时,不会抛出任何错误,但不会填充数据。我很确定我的频率字段是导致问题的,但是无法提供解决方案。
根据用户的“通知名称”选择,用户可以选择两种不同类型的频率。一个选择具有3个单独的字段,用于数值,时间范围(周,月等)以及选择之前/之后。另一个简单地将瞬时声明为静态文本字段。无论选择哪个选项,频率数据都应填充到数据库中的一个单元格中,然后在必要时使用管道进行分隔。我仍然是C#MVC的新手,所以非常感谢任何帮助。
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,notificationType1,recipientTypeId,frequency")] NotificationType notificationType)
{
if (ModelState.IsValid)
{
db.NotificationType.Add(notificationType);
db.SaveChanges();
return RedirectToAction("Create");
}
ViewBag.recipientTypeId = new SelectList(db.RecipientType, "Id", "recipientRole", notificationType.recipientTypeId);
return View(notificationType);
}
查看
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.notificationType1, "Notification Name", htmlAttributes: new { @class = "control-label col-md-2 helper-format" })
<div class="col-md-10" id="type_selection">
@Html.DropDownList("notificationType1", new List<SelectListItem> {
new SelectListItem { Text = "Make a Selection", Value="" },
new SelectListItem { Text = "Incomplete Documents", Value= "Incomplete Documents" },
new SelectListItem { Text = "All Documents Complete", Value = "All Documents Complete" },
new SelectListItem { Text = "Documents Requiring Action", Value = "Documents Requiring Action" }
}, new { @class = "helper-format", @id = "value_select", style = "font-family: 'Roboto', Sans Serif;" })
@Html.ValidationMessageFor(model => model.notificationType1, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group" id="frequency_group">
@Html.LabelFor(model => model.frequency, "Frequency", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-sm-3" id="frequency_group">
@Html.TextBoxFor(model => model.frequency, new { @class = "textbox-width", @placeholder = "42" })
@Html.DropDownList("frequency", new List<SelectListItem>
{
new SelectListItem { Text = "Day(s)", Value= "| Day"},
new SelectListItem { Text = "Week(s)", Value= "| Week"},
new SelectListItem { Text = "Month(s)", Value= "| Month"}
})
@Html.DropDownList("frequency", new List<SelectListItem>
{
new SelectListItem { Text = "Before", Value= "| Before"},
new SelectListItem { Text = "After", Value= "| After"}
})
</div>
<p class="col-sm-2" id="psdatetext">The Beginning</p>
</div>
<div class="form-group" id="freq_instant">
@Html.LabelFor(model => model.frequency, "Frequency", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="instant_text">
<p>Instantaneous</p></div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.recipientTypeId, "Notification For", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("recipientTypeId", new List<SelectListItem>
{
new SelectListItem { Text = "Me", Value= "Me"},
new SelectListItem { Text = "Account Manager", Value="Account Manager" },
new SelectListItem { Text = "Candidate", Value= "Candidate"},
new SelectListItem { Text = "Recruiter", Value="Recruiter" },
new SelectListItem { Text = "Manager", Value= "Manager"}
})
</div>
</div>
<div class="form-group">
<div class="col-md-offset-1 col-md-10">
<div id="hovercreate">
<button type="submit" value="CREATE" class="btn btn-primary" id="createbtn">CREATE</button>
</div>
</div>
</div>
</div>
}
JS用于频率选项
@Scripts.Render("~/bundles/jquery")
<script type="text/javascript">
$(document).ready(function () {
$('#frequency_group').hide()
$('#freq_instant').hide()
$('#value_select').change(function () {
var selection = $('#value_select').val();
$('#frequency_group').hide();
switch (selection) {
case 'Incomplete Documents':
$('#frequency_group').show();
break;
case 'All Documents Complete':
$('#frequency_group').show();
break;
}
});
$('#value_select').on('change', function () {
if (this.value == 'Documents Requiring Action') {
$("#freq_instant").show();
}
else {
$("#freq_instant").hide();
}
});
});
答案 0 :(得分:1)
是否手动分配了Id键?如果不是(例如,如果它是IDENTITY字段),则不应该绑定它 - 从[Bind(Include =&#34; ...&#34;)]中删除Id。 / p>
答案 1 :(得分:1)
您是否在方法上设置了断点?如果是这样,它会触发吗?
如果没有,试试这个......
根据我的记忆,所有控制器都有一个默认参数ID,它在RouteConfig.cs文件中设置(App_Start / RouteConfig.cs)。
有几种方法可以从那里开始。 1.为控制器提供ID参数(例如(int ID)) 2.通过Route属性
设置路径值要做到这一点,你需要 - A.在RouteConfig.cs / RegisterRoutes方法的顶部添加以下内容。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
//...
}
B中。添加
[ValidateAntiForgeryToken]
[Route(@"Create/")]
public ActionResult Create([Bind(Include = ...
{
我还建议在方法的开头设置一个断点,看它是否会触及它。
http://www.tutorialsteacher.com/mvc/routing-in-mvc https://msdn.microsoft.com/en-us/library/system.web.mvc.routecollectionattributeroutingextensions.mapmvcattributeroutes%28v=vs.118%29.aspx