我有一个局部视图,我在另一个视图中调用。局部视图包含一个如下所示的表单:
@model Solnet_HotelSuite.Models.UserModels
@using (Html.BeginForm("registration", "User", FormMethod.Post, new { id="UserReg"}))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>UserModels</legend>
<div id="submitResult">
@ViewBag.res
</div>
<div class="fields_container">
<div class="fields_labels">
@Html.LabelFor(model => model.user_name)
</div>
<div class="fields">
@Html.EditorFor(model => model.user_name)
@Html.ValidationMessageFor(model => model.user_name)
</div>
</div>
<div class="fields_container">
<div class="fields_labels">
@Html.LabelFor(model => model.user_pass)
</div>
<div class="fields">
@Html.EditorFor(model => model.user_pass)
@Html.ValidationMessageFor(model => model.user_pass)
</div>
</div>
<div class="fields_container">
<div class="fields_labels">
@Html.LabelFor(model => model.UserEmail)
</div>
<div class="fields">
@Html.EditorFor(model => model.UserEmail)
@Html.ValidationMessageFor(model => model.UserEmail)
</div>
</div>
<div class="fields_container">
<div class="fields_labels">
@Html.LabelFor(model => model.RegDate)
</div>
<div class="fields">
@Html.EditorFor(model => model.RegDate)
@Html.ValidationMessageFor(model => model.RegDate)
</div>
</div>
<div class="fields_container">
<div class="fields_labels">
@Html.LabelFor(model => model.status)
</div>
<div class="fields">
@Html.EditorFor(model => model.status)
@Html.ValidationMessageFor(model => model.status)
</div>
</div>
<div class="fields_container">
<div class="fields_labels">
User Role
</div>
<div class="fields">
@{
var md = new Solnet_HotelSuite.Models.DBEntity();
var umod = md.RolesModels.Select(r => r.Caption);
@Html.DropDownList("userID", new SelectList(umod), new { @id = "userID" })
@Html.ValidationMessageFor(model => model.Roles.Caption)
}
</div>
</div>
<p>
<button value="Create" id="register_USER">Save</button>
</p>
</fieldset>
}
@Content.Script("jquery-1.6.2.js", Url)
<script type="text/javascript" >
$(function () {
$('#register_USER').click(function () {
var form = $('#UserReg');
// alert(JSON.stringify(form.serialize()));
$.ajax({
type: "POST",
url: form.attr('action'),
data: JSON.stringify(form.serialize()),
dataType: "json",
contentType: "application/json",
success: function (data) {
alert(data.Message);
},
error: function (data) {
console.log(data.Message);
alert(data.Message);
}
});
return false;
});
});
</script>
并且控制器操作如下所示:
[HttpPost]
public JsonResult registration(Solnet_HotelSuite.Models.UserModels user, string userID)
{
try
{
if (ModelState.IsValid)
{
if (userExist(user.user_name))
{
ViewBag.res = "User exist in record";
return Json(new { Message = "User Registartion failed" });
}
}
//var crypto = new SimpleCrypto.PBKDF2();
using (var db = new DBEntity())
{
var query = (from p in db.RolesModels
where p.Caption == userID
select p.RoleId).Single();
var sysUser = db.UserModels.Create();
sysUser.user_name = user.user_name;
sysUser.user_pass = user.user_pass;
sysUser.UserEmail = user.UserEmail;
sysUser.RegDate = user.RegDate;
sysUser.RoleId = query;
sysUser.Roles = user.Roles;
sysUser.status = user.status;
db.UserModels.Add(sysUser);
db.SaveChanges();
}
var db1 = new DBEntity();
ViewBag.Roles = db1.RolesModels.Select(r => r.Caption);
ViewBag.RolesId = db1.RolesModels.Select(r => r.RoleId);
}
catch (DbEntityValidationException dbEx)
{
}
if (!Request.IsAjaxRequest())
{
return Json(new { Message = "User Registartion successful"});
}
else
{
return Json(new { Message = "User Registartion failed" });
}
}
我一直在警报中“未定义”。我不知道我做错了什么。
答案 0 :(得分:1)
对所有yor逻辑使用IsValid条件。 尝试Catch应该在你的savechanges方法上。
使用use来处理你的连接
[HttpPost]
public JsonResult registration(Solnet_HotelSuite.Models.UserModels user, string userID)
{
try
{
if (ModelState.IsValid)
{
if (userExist(user.user_name))
{
ViewBag.res = "User exist in record";
return Json(new { Message = "User Registartion failed" });
}
using (var db = new DBEntity())
{
var query = (from p in db.RolesModels
where p.Caption == userID
select p.RoleId).Single();
var sysUser = db.UserModels.Create();
sysUser.user_name = user.user_name;
sysUser.user_pass = user.user_pass;
sysUser.UserEmail = user.UserEmail;
sysUser.RegDate = user.RegDate;
sysUser.RoleId = query;
sysUser.Roles = user.Roles;
sysUser.status = user.status;
db.UserModels.Add(sysUser);
try{
db.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
return Json(new { Message = "User Registartion failed" });
}
}
using (var db = new DBEntity())
{
ViewBag.Roles = db.RolesModels.Select(r => r.Caption);
ViewBag.RolesId = db.RolesModels.Select(r => r.RoleId);
}
return Json(new { Message = "User Registartion successful"});
}
else
return Json(new { Message = "User Registartion failed" });
}
}
}