Asp.net mvc 为什么在asp.net mvc中更新数据时if语句总是返回false?
我用这段代码在Asp.net mvc 为什么在asp.net mvc中更新数据时if语句总是返回false?,asp.net-mvc,if-statement,asp.net-mvc-5,sql-update,updatemodel,Asp.net Mvc,If Statement,Asp.net Mvc 5,Sql Update,Updatemodel,我用这段代码在asp.net mvc 5中更新用户数据,我认为一切都正常,但我不知道为什么会收到错误消息,数据没有保存,并且如果控制器中的(ModelState.IsValid)始终为false。 谁能帮我一下吗 管理员控制器 [HttpGet] public ActionResult EditUser(int id) { var load = db.Users.Find(id); return View(load); } pri
asp.net mvc 5
中更新用户数据,我认为一切都正常,但我不知道为什么会收到错误消息,数据没有保存,并且如果控制器中的(ModelState.IsValid)
始终为false
。
谁能帮我一下吗
管理员控制器
[HttpGet]
public ActionResult EditUser(int id)
{
var load = db.Users.Find(id);
return View(load);
}
private const string _ImagesPathUser = "~/Images/User";
[HttpPost]
public ActionResult EditUser(User user, HttpPostedFileBase UploadImage)
{
if (ModelState.IsValid) // always returns false
{
UserRepositories blUser = new UserRepositories();
if (UploadImage != null)
{
// Delete exiting file
System.IO.File.Delete(Path.Combine(Server.MapPath(_ImagesPathUser), user.UserImage));
// Save new file
string fileName = Guid.NewGuid() + Path.GetFileName(UploadImage.FileName);
string path = Path.Combine(Server.MapPath(_ImagesPathUser), fileName);
UploadImage.SaveAs(path);
user.UserImage = fileName;
}
if (blUser.Update(user))
{
return JavaScript("alert(' added');");
}
else
{
return JavaScript("alert(' didn't add');");
}
}
else
{
return JavaScript("alert('Error');");
}
}
UserRepositories.cs
public bool Delete(int id, bool autoSave = true)
{
try
{
var entity = db.Users.Find(id);
db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
if (autoSave)
{
bool result = Convert.ToBoolean(db.SaveChanges());
if (result)
{
try
{
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\User\\" + entity.UserImage) == true)
{
File.Delete(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\User\\" + entity.UserImage);
}
}
catch { }
}
return result;
}
else
return false;
}
catch
{
return false;
}
}
@model NP1.Models.User
@{
ViewBag.Title = "EditUser";
Layout = "~/Views/Admin/AdminLayout.cshtml";
}
<h2>EditUser</h2>
@using (Html.BeginForm("EditUser", "Admin", FormMethod.Post, new { enctype = "multipart/form-data", id = "myUploadForm5" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>User</h4>
<hr />
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.UserID)
<div class="form-group">
@Html.LabelFor(model => model.UserEmail, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserEmail)
@Html.ValidationMessageFor(model => model.UserEmail)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserFirstName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserFirstName)
@Html.ValidationMessageFor(model => model.UserFirstName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserLastName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserLastName)
@Html.ValidationMessageFor(model => model.UserLastName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserPassWord, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserPassWord)
@Html.ValidationMessageFor(model => model.UserPassWord)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserCellPhone, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserCellPhone)
@Html.ValidationMessageFor(model => model.UserCellPhone)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserTell, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserTell)
@Html.ValidationMessageFor(model => model.UserTell)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserImage, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.ImageFor(model => model.UserImage, new { width = "300" }, "", "Images", "User")
@Html.Upload("UploadImage")
@Html.HiddenFor(model => model.UserImage)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserAddress, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserAddress)
@Html.ValidationMessageFor(model => model.UserAddress)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserBirthDate, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserBirthDate)
@Html.ValidationMessageFor(model => model.UserBirthDate)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserGender, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserGender)
@Html.ValidationMessageFor(model => model.UserGender)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
public partial class User
{
public int UserID { get; set; }
public string UserEmail { get; set; }
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public string UserPassWord { get; set; }
public string UserCellPhone { get; set; }
public string UserTell { get; set; }
public string UserImage { get; set; }
public string UserAddress { get; set; }
public Nullable<byte> UserStatus { get; set; }
public Nullable<System.DateTime> UserBirthDate { get; set; }
public string UserGender { get; set; }
}
internal class UserMetaData
{
[ScaffoldColumn(false)]
[Bindable(false)]
public int UserID { get; set; }
[Required(ErrorMessage = "enter email", AllowEmptyStrings = false)]
[DisplayName("email")]
[Display(Name = "email")]
[RegularExpression(@"^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,4})$", ErrorMessage = "enter correct email")]
public string UserEmail { get; set; }
[Required(ErrorMessage = "enter your name", AllowEmptyStrings = false)]
[DisplayName("name")]
[Display(Name = "name")]
[StringLength(50, ErrorMessage = "should be 50")]
public string UserFirstName { get; set; }
[Required(ErrorMessage = "enter your last name", AllowEmptyStrings = false)]
[DisplayName("last name")]
[Display(Name = "last name")]
[StringLength(50, ErrorMessage = "should be 50")]
public string UserLastName { get; set; }
[Required(ErrorMessage = "enter password", AllowEmptyStrings = false)]
[Display(Name = "password")]
[DisplayName("password")]
[DataType(DataType.Password)]
public string UserPassWord { get; set; }
[Display(Name = "mobile")]
[DisplayName("mobile")]
[RegularExpression(@"^0?9[123]\d{8}$", ErrorMessage = "enter mobile correct")]
[StringLength(11, ErrorMessage = "should be 11")]
public string UserCellPhone { get; set; }
[Display(Name = "tel")]
[DisplayName("tel")]
[StringLength(11, ErrorMessage = "should be 11")]
public string UserTell { get; set; }
public string UserImage { get; set; }
public string UserAddress { get; set; }
[ScaffoldColumn(false)]
[Display(Name = "status")]
[DisplayName("status")]
public Nullable<byte> UserStatus { get; set; }
[Display(Name = "BirthDate")]
[DisplayName("BirthDate")]
public Nullable<System.DateTime> UserBirthDate { get; set; }
[Display(Name = "gender")]
[DisplayName("gender")]
public string UserGender { get; set; }
}
}
namespace NP1.Models
{
[MetadataType(typeof(NP1.Models.MetaData.UserMetaData))]
public partial class User
{
[Required(ErrorMessage = "enter your pass", AllowEmptyStrings = false)]
[Display(Name = "repeate pass")]
[DisplayName("repeate pass")]
[DataType(DataType.Password)]
[Compare("UserPassWord", ErrorMessage = "not equal")]
public string UserConfirmPassWord { get; set; }
public int Year { get; set; }
public int Month { get; set; }
public int Day { get; set; }
}
EditUser.cs
public bool Delete(int id, bool autoSave = true)
{
try
{
var entity = db.Users.Find(id);
db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
if (autoSave)
{
bool result = Convert.ToBoolean(db.SaveChanges());
if (result)
{
try
{
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\User\\" + entity.UserImage) == true)
{
File.Delete(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\User\\" + entity.UserImage);
}
}
catch { }
}
return result;
}
else
return false;
}
catch
{
return false;
}
}
@model NP1.Models.User
@{
ViewBag.Title = "EditUser";
Layout = "~/Views/Admin/AdminLayout.cshtml";
}
<h2>EditUser</h2>
@using (Html.BeginForm("EditUser", "Admin", FormMethod.Post, new { enctype = "multipart/form-data", id = "myUploadForm5" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>User</h4>
<hr />
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.UserID)
<div class="form-group">
@Html.LabelFor(model => model.UserEmail, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserEmail)
@Html.ValidationMessageFor(model => model.UserEmail)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserFirstName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserFirstName)
@Html.ValidationMessageFor(model => model.UserFirstName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserLastName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserLastName)
@Html.ValidationMessageFor(model => model.UserLastName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserPassWord, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserPassWord)
@Html.ValidationMessageFor(model => model.UserPassWord)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserCellPhone, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserCellPhone)
@Html.ValidationMessageFor(model => model.UserCellPhone)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserTell, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserTell)
@Html.ValidationMessageFor(model => model.UserTell)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserImage, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.ImageFor(model => model.UserImage, new { width = "300" }, "", "Images", "User")
@Html.Upload("UploadImage")
@Html.HiddenFor(model => model.UserImage)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserAddress, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserAddress)
@Html.ValidationMessageFor(model => model.UserAddress)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserBirthDate, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserBirthDate)
@Html.ValidationMessageFor(model => model.UserBirthDate)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserGender, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserGender)
@Html.ValidationMessageFor(model => model.UserGender)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
public partial class User
{
public int UserID { get; set; }
public string UserEmail { get; set; }
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public string UserPassWord { get; set; }
public string UserCellPhone { get; set; }
public string UserTell { get; set; }
public string UserImage { get; set; }
public string UserAddress { get; set; }
public Nullable<byte> UserStatus { get; set; }
public Nullable<System.DateTime> UserBirthDate { get; set; }
public string UserGender { get; set; }
}
internal class UserMetaData
{
[ScaffoldColumn(false)]
[Bindable(false)]
public int UserID { get; set; }
[Required(ErrorMessage = "enter email", AllowEmptyStrings = false)]
[DisplayName("email")]
[Display(Name = "email")]
[RegularExpression(@"^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,4})$", ErrorMessage = "enter correct email")]
public string UserEmail { get; set; }
[Required(ErrorMessage = "enter your name", AllowEmptyStrings = false)]
[DisplayName("name")]
[Display(Name = "name")]
[StringLength(50, ErrorMessage = "should be 50")]
public string UserFirstName { get; set; }
[Required(ErrorMessage = "enter your last name", AllowEmptyStrings = false)]
[DisplayName("last name")]
[Display(Name = "last name")]
[StringLength(50, ErrorMessage = "should be 50")]
public string UserLastName { get; set; }
[Required(ErrorMessage = "enter password", AllowEmptyStrings = false)]
[Display(Name = "password")]
[DisplayName("password")]
[DataType(DataType.Password)]
public string UserPassWord { get; set; }
[Display(Name = "mobile")]
[DisplayName("mobile")]
[RegularExpression(@"^0?9[123]\d{8}$", ErrorMessage = "enter mobile correct")]
[StringLength(11, ErrorMessage = "should be 11")]
public string UserCellPhone { get; set; }
[Display(Name = "tel")]
[DisplayName("tel")]
[StringLength(11, ErrorMessage = "should be 11")]
public string UserTell { get; set; }
public string UserImage { get; set; }
public string UserAddress { get; set; }
[ScaffoldColumn(false)]
[Display(Name = "status")]
[DisplayName("status")]
public Nullable<byte> UserStatus { get; set; }
[Display(Name = "BirthDate")]
[DisplayName("BirthDate")]
public Nullable<System.DateTime> UserBirthDate { get; set; }
[Display(Name = "gender")]
[DisplayName("gender")]
public string UserGender { get; set; }
}
}
namespace NP1.Models
{
[MetadataType(typeof(NP1.Models.MetaData.UserMetaData))]
public partial class User
{
[Required(ErrorMessage = "enter your pass", AllowEmptyStrings = false)]
[Display(Name = "repeate pass")]
[DisplayName("repeate pass")]
[DataType(DataType.Password)]
[Compare("UserPassWord", ErrorMessage = "not equal")]
public string UserConfirmPassWord { get; set; }
public int Year { get; set; }
public int Month { get; set; }
public int Day { get; set; }
}
@model NP1.Models.User
@{
ViewBag.Title=“EditUser”;
Layout=“~/Views/Admin/AdminLayout.cshtml”;
}
编辑用户
@使用(Html.BeginForm(“EditUser”,“Admin”,FormMethod.Post,new{enctype=“multipart/formdata”,id=“myUploadForm5”}))
{
@Html.AntiForgeryToken()
使用者
@Html.ValidationSummary(true)
@Html.HiddenFor(model=>model.UserID)
@LabelFor(model=>model.UserEmail,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.UserEmail)
@Html.ValidationMessageFor(model=>model.UserEmail)
@LabelFor(model=>model.UserFirstName,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.UserFirstName)
@Html.ValidationMessageFor(model=>model.UserFirstName)
@LabelFor(model=>model.UserLastName,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.UserLastName)
@Html.ValidationMessageFor(model=>model.UserLastName)
@LabelFor(model=>model.UserPassWord,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.UserPassWord)
@Html.ValidationMessageFor(model=>model.UserPassWord)
@Html.LabelFor(model=>model.userphone,新的{@class=“控制标签col-md-2”})
@EditorFor(model=>model.usermobile)
@Html.ValidationMessageFor(model=>model.usermobile)
@LabelFor(model=>model.UserTell,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.UserTell)
@Html.ValidationMessageFor(model=>model.UserTell)
@LabelFor(model=>model.UserImage,新的{@class=“controllabel col-md-2”})
@Html.ImageFor(model=>model.UserImage,新的{width=“300”},“,“Images”,“User”)
@上传(“上传图像”)
@Html.HiddenFor(model=>model.UserImage)
@LabelFor(model=>model.UserAddress,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.UserAddress)
@Html.ValidationMessageFor(model=>model.UserAddress)
@LabelFor(model=>model.UserBirthDate,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.UserBirthDate)
@Html.ValidationMessageFor(model=>model.UserBirthDate)
@LabelFor(model=>model.UserGender,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.UserGender)
@Html.ValidationMessageFor(model=>model.UserGender)
}
User.cs
public bool Delete(int id, bool autoSave = true)
{
try
{
var entity = db.Users.Find(id);
db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
if (autoSave)
{
bool result = Convert.ToBoolean(db.SaveChanges());
if (result)
{
try
{
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\User\\" + entity.UserImage) == true)
{
File.Delete(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\User\\" + entity.UserImage);
}
}
catch { }
}
return result;
}
else
return false;
}
catch
{
return false;
}
}
@model NP1.Models.User
@{
ViewBag.Title = "EditUser";
Layout = "~/Views/Admin/AdminLayout.cshtml";
}
<h2>EditUser</h2>
@using (Html.BeginForm("EditUser", "Admin", FormMethod.Post, new { enctype = "multipart/form-data", id = "myUploadForm5" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>User</h4>
<hr />
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.UserID)
<div class="form-group">
@Html.LabelFor(model => model.UserEmail, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserEmail)
@Html.ValidationMessageFor(model => model.UserEmail)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserFirstName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserFirstName)
@Html.ValidationMessageFor(model => model.UserFirstName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserLastName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserLastName)
@Html.ValidationMessageFor(model => model.UserLastName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserPassWord, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserPassWord)
@Html.ValidationMessageFor(model => model.UserPassWord)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserCellPhone, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserCellPhone)
@Html.ValidationMessageFor(model => model.UserCellPhone)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserTell, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserTell)
@Html.ValidationMessageFor(model => model.UserTell)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserImage, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.ImageFor(model => model.UserImage, new { width = "300" }, "", "Images", "User")
@Html.Upload("UploadImage")
@Html.HiddenFor(model => model.UserImage)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserAddress, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserAddress)
@Html.ValidationMessageFor(model => model.UserAddress)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserBirthDate, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserBirthDate)
@Html.ValidationMessageFor(model => model.UserBirthDate)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserGender, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserGender)
@Html.ValidationMessageFor(model => model.UserGender)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
public partial class User
{
public int UserID { get; set; }
public string UserEmail { get; set; }
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public string UserPassWord { get; set; }
public string UserCellPhone { get; set; }
public string UserTell { get; set; }
public string UserImage { get; set; }
public string UserAddress { get; set; }
public Nullable<byte> UserStatus { get; set; }
public Nullable<System.DateTime> UserBirthDate { get; set; }
public string UserGender { get; set; }
}
internal class UserMetaData
{
[ScaffoldColumn(false)]
[Bindable(false)]
public int UserID { get; set; }
[Required(ErrorMessage = "enter email", AllowEmptyStrings = false)]
[DisplayName("email")]
[Display(Name = "email")]
[RegularExpression(@"^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,4})$", ErrorMessage = "enter correct email")]
public string UserEmail { get; set; }
[Required(ErrorMessage = "enter your name", AllowEmptyStrings = false)]
[DisplayName("name")]
[Display(Name = "name")]
[StringLength(50, ErrorMessage = "should be 50")]
public string UserFirstName { get; set; }
[Required(ErrorMessage = "enter your last name", AllowEmptyStrings = false)]
[DisplayName("last name")]
[Display(Name = "last name")]
[StringLength(50, ErrorMessage = "should be 50")]
public string UserLastName { get; set; }
[Required(ErrorMessage = "enter password", AllowEmptyStrings = false)]
[Display(Name = "password")]
[DisplayName("password")]
[DataType(DataType.Password)]
public string UserPassWord { get; set; }
[Display(Name = "mobile")]
[DisplayName("mobile")]
[RegularExpression(@"^0?9[123]\d{8}$", ErrorMessage = "enter mobile correct")]
[StringLength(11, ErrorMessage = "should be 11")]
public string UserCellPhone { get; set; }
[Display(Name = "tel")]
[DisplayName("tel")]
[StringLength(11, ErrorMessage = "should be 11")]
public string UserTell { get; set; }
public string UserImage { get; set; }
public string UserAddress { get; set; }
[ScaffoldColumn(false)]
[Display(Name = "status")]
[DisplayName("status")]
public Nullable<byte> UserStatus { get; set; }
[Display(Name = "BirthDate")]
[DisplayName("BirthDate")]
public Nullable<System.DateTime> UserBirthDate { get; set; }
[Display(Name = "gender")]
[DisplayName("gender")]
public string UserGender { get; set; }
}
}
namespace NP1.Models
{
[MetadataType(typeof(NP1.Models.MetaData.UserMetaData))]
public partial class User
{
[Required(ErrorMessage = "enter your pass", AllowEmptyStrings = false)]
[Display(Name = "repeate pass")]
[DisplayName("repeate pass")]
[DataType(DataType.Password)]
[Compare("UserPassWord", ErrorMessage = "not equal")]
public string UserConfirmPassWord { get; set; }
public int Year { get; set; }
public int Month { get; set; }
public int Day { get; set; }
}
公共部分类用户
{
public int UserID{get;set;}
公共字符串UserEmail{get;set;}
公共字符串UserFirstName{get;set;}
公共字符串UserLastName{get;set;}
公共字符串UserPassWord{get;set;}
公共字符串userphone{get;set;}
公共字符串UserTell{get;set;}
公共字符串UserImage{get;set;}
公共字符串用户地址{get;set;}
公共可空用户状态{get;set;}
公共可为空的UserBirthDate{get;set;}
公共字符串UserGender{get;set;}
}
UserMetaData.cs
public bool Delete(int id, bool autoSave = true)
{
try
{
var entity = db.Users.Find(id);
db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
if (autoSave)
{
bool result = Convert.ToBoolean(db.SaveChanges());
if (result)
{
try
{
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\User\\" + entity.UserImage) == true)
{
File.Delete(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\User\\" + entity.UserImage);
}
}
catch { }
}
return result;
}
else
return false;
}
catch
{
return false;
}
}
@model NP1.Models.User
@{
ViewBag.Title = "EditUser";
Layout = "~/Views/Admin/AdminLayout.cshtml";
}
<h2>EditUser</h2>
@using (Html.BeginForm("EditUser", "Admin", FormMethod.Post, new { enctype = "multipart/form-data", id = "myUploadForm5" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>User</h4>
<hr />
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.UserID)
<div class="form-group">
@Html.LabelFor(model => model.UserEmail, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserEmail)
@Html.ValidationMessageFor(model => model.UserEmail)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserFirstName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserFirstName)
@Html.ValidationMessageFor(model => model.UserFirstName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserLastName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserLastName)
@Html.ValidationMessageFor(model => model.UserLastName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserPassWord, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserPassWord)
@Html.ValidationMessageFor(model => model.UserPassWord)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserCellPhone, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserCellPhone)
@Html.ValidationMessageFor(model => model.UserCellPhone)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserTell, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserTell)
@Html.ValidationMessageFor(model => model.UserTell)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserImage, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.ImageFor(model => model.UserImage, new { width = "300" }, "", "Images", "User")
@Html.Upload("UploadImage")
@Html.HiddenFor(model => model.UserImage)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserAddress, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserAddress)
@Html.ValidationMessageFor(model => model.UserAddress)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserBirthDate, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserBirthDate)
@Html.ValidationMessageFor(model => model.UserBirthDate)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserGender, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserGender)
@Html.ValidationMessageFor(model => model.UserGender)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
public partial class User
{
public int UserID { get; set; }
public string UserEmail { get; set; }
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public string UserPassWord { get; set; }
public string UserCellPhone { get; set; }
public string UserTell { get; set; }
public string UserImage { get; set; }
public string UserAddress { get; set; }
public Nullable<byte> UserStatus { get; set; }
public Nullable<System.DateTime> UserBirthDate { get; set; }
public string UserGender { get; set; }
}
internal class UserMetaData
{
[ScaffoldColumn(false)]
[Bindable(false)]
public int UserID { get; set; }
[Required(ErrorMessage = "enter email", AllowEmptyStrings = false)]
[DisplayName("email")]
[Display(Name = "email")]
[RegularExpression(@"^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,4})$", ErrorMessage = "enter correct email")]
public string UserEmail { get; set; }
[Required(ErrorMessage = "enter your name", AllowEmptyStrings = false)]
[DisplayName("name")]
[Display(Name = "name")]
[StringLength(50, ErrorMessage = "should be 50")]
public string UserFirstName { get; set; }
[Required(ErrorMessage = "enter your last name", AllowEmptyStrings = false)]
[DisplayName("last name")]
[Display(Name = "last name")]
[StringLength(50, ErrorMessage = "should be 50")]
public string UserLastName { get; set; }
[Required(ErrorMessage = "enter password", AllowEmptyStrings = false)]
[Display(Name = "password")]
[DisplayName("password")]
[DataType(DataType.Password)]
public string UserPassWord { get; set; }
[Display(Name = "mobile")]
[DisplayName("mobile")]
[RegularExpression(@"^0?9[123]\d{8}$", ErrorMessage = "enter mobile correct")]
[StringLength(11, ErrorMessage = "should be 11")]
public string UserCellPhone { get; set; }
[Display(Name = "tel")]
[DisplayName("tel")]
[StringLength(11, ErrorMessage = "should be 11")]
public string UserTell { get; set; }
public string UserImage { get; set; }
public string UserAddress { get; set; }
[ScaffoldColumn(false)]
[Display(Name = "status")]
[DisplayName("status")]
public Nullable<byte> UserStatus { get; set; }
[Display(Name = "BirthDate")]
[DisplayName("BirthDate")]
public Nullable<System.DateTime> UserBirthDate { get; set; }
[Display(Name = "gender")]
[DisplayName("gender")]
public string UserGender { get; set; }
}
}
namespace NP1.Models
{
[MetadataType(typeof(NP1.Models.MetaData.UserMetaData))]
public partial class User
{
[Required(ErrorMessage = "enter your pass", AllowEmptyStrings = false)]
[Display(Name = "repeate pass")]
[DisplayName("repeate pass")]
[DataType(DataType.Password)]
[Compare("UserPassWord", ErrorMessage = "not equal")]
public string UserConfirmPassWord { get; set; }
public int Year { get; set; }
public int Month { get; set; }
public int Day { get; set; }
}
内部类UserMetaData
{
[脚手架立柱(假)]
[可装订(假)]
public int UserID{get;set;}
[必需(ErrorMessage=“输入电子邮件”,AllowEmptyStrings=false)]
[显示名称(“电子邮件”)]
[显示(Name=“电子邮件”)]
[RegularExpression(@“[U A-Za-z0-9-\+])+(\.[U A-Za-z0-9-]+)*[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,4})$”,ErrorMessage=“输入正确的电子邮件”)]
公共字符串UserEmail{get;set;}
[必需(ErrorMessage=“输入您的姓名”,AllowEmptyStrings=false)]
[显示名称(“名称”)]
[显示(Name=“Name”)]
[StringLength(50,ErrorMessage=“应为50”)]
公共字符串UserFirstName{get;set;}
[必需(ErrorMessage=“输入您的姓氏”,AllowEmptyStrings=false)]
[显示名称(“姓氏”)]
[显示(Name=“last Name”)]
[StringLength(50,ErrorMessage=“应为50”)]
公共字符串UserLastName{get;set;}
[必需(ErrorMessage=“输入密码”,AllowEmptyStrings=false)]
[显示(Name=“密码”)]
[显示名称(“密码”)]
[数据类型(数据类型.密码)]
公共字符串UserPassWord{get;set;}
[显示(Name=“移动”)]
[显示名称(“移动”)]
[RegularExpression(@“^0?9[123]\d{8}$”,ErrorMessage=“enter-mobile-correct”)]
[StringLength(11,ErrorMessage=“应该是11”)]
公共字符串userphone{get;set;}
[显示(Name=“tel”)]
[显示名称(“电话”)]
[StringLength(11,ErrorMessage=“应该是11”)]
公共字符串UserTell{get;set;}
公共字符串UserImage{get;set;}
公共字符串用户地址{get;set;}
[脚手架立柱(假)]
[显示(Name=“status”)]
[显示名称(“状态”)]
公共可空用户状态{get;set;}
[显示(Name=“生日”)]
[显示姓名(“生日”)]
公共可为空的UserBirthDate{get;set;}
[显示(Name=“gender”)]
[显示名称(“性别”)]
公共街