[HttpPost]
public ActionResult AddMsg(MsgModel model)
{
string strSql = "insert into tbl_msg(title,msg,msg_by,msg_date,status) values(@title,@msg,@msg_by,getdate(),'A')";
SqlParameter[] paramter ={
new SqlParameter("@title",SqlDbType.NVarChar,200),
new SqlParameter("@msg",SqlDbType.NVarChar,1000),
new SqlParameter("@msg_by",SqlDbType.VarChar,20)
};
paramter[0].Value = model.Title;
paramter[1].Value = model.Msg;
paramter[2].Value = LoginStaffID;
if (SqlDbHelper.ExecuteSql(strSql, paramter) > 0)
{
TempData["SuccKey"] = "添加成功,是否继续添加呢?";//前台弹出JS
}
else
{
ModelState.AddModelError("addError", "发生错误,留言失败!");//文本显示
return View(model);
}
return View();
}
<!DOCTYPE html>
<html>
<head>
<title>添加留言</title>
<link type="text/css" href="@Url.Content("~/Content/Site.css")" rel="Stylesheet" />
</head>
<body>
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>MsgModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Msg)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Msg)
@Html.ValidationMessageFor(model => model.Msg)
</div>
用户名:@Html.DropDownList("ddlStaff_id", (SelectList)ViewBag.list, "请选择")
<p>
<input type="submit" value="添加留言" />
@Html.ValidationMessage("addError")
</p>
</fieldset>
}
@if (TempData["SuccKey"]!=null)
{
<script type="text/javascript">
if (!confirm('@HttpUtility.JavaScriptStringEncode(Convert.ToString(TempData["SuccKey"]))')) {
window.location.href="Index";
}
</script>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</body>
</html>
运行效果如下:
<form action="/Msg/AddMsg" method="post">
.......
<p>
<input type="submit" value="添加留言" />
<span class="field-validation-valid" data-valmsg-for="addError" data-valmsg-replace="true"></span>
</p>
</form>
<script type="text/javascript">
if (!confirm('添加成功,是否继续添加呢?')) {
window.location.href="Index";
}
</script>
<a href="/Msg">Back to List</a>