我正试图让登录用户编辑其个人资料。现在,我决定使用一个固定的userID (因为使用会话useID也会给我带来错误)。现在,当我运行代码时,它遍历了所有行,但最后,它给了我这个错误“ EntityValidationErrors”。
我只希望代码更新当前UserID的简历,性别和首选性别
[HttpPost]
public ActionResult FillProfile(tblUser user)
{
using (TrinityEntities db = new TrinityEntities())
{
var id = Session["userID"];
user.Id = 1018; //Convert.ToInt32(id);
SqlConnection conn = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\Ruben\Desktop\Periode 2\Persoonlijk\Trinity\Trinity\Trinity\App_Data\Trinity.mdf; Integrated Security = True; MultipleActiveResultSets = True; Connect Timeout = 30; Application Name = EntityFramework");
conn.Open();
string Query = "SELECT FirstName, LastName, Email, Password, Age FROM tblUser WHERE Id='" + user.Id + "'";
using (SqlCommand command = new SqlCommand(Query, conn))
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
user.FirstName = reader[0] as string;
user.LastName = reader[1] as string;
user.Email = reader[2] as string;
user.Password = reader[3] as string;
string Sage = reader[4] as string;
user.Age = Convert.ToInt32(Sage);
break;
}
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
}
conn.Close();
return RedirectToAction("Index", "Home");
}
}
查看:
@model Trinity.Models.tblUser
@{
ViewBag.Title = "Profile";
// prevent login by alterring adress
if (Session["userID"] == null)
{
Response.Redirect("~/Login/Index");
}
}
<h2>Let's fill you profile!</h2>
@using (Html.BeginForm("FillProfile", "Profile", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Sex, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Sex,
new List<SelectListItem> {
new SelectListItem { Value = "Male" , Text = "Male" },
new SelectListItem { Value = "Female" , Text = "Female" },
},
new {@class = "control-label col-md-2" })
@Html.ValidationMessageFor(model => model.Sex, "", new { @class = "text-danger" })
</div>
</div>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Preffered, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Preffered,
new List<SelectListItem> {
new SelectListItem { Value = "Female" , Text = "Female" },
new SelectListItem { Value = "Male" , Text = "Male" },
new SelectListItem { Value = "Both" , Text = "Both" }
},
new {@class = "control-label col-md-2" })
@Html.ValidationMessageFor(model => model.Preffered, "", new { @class = "text-danger" })
</div>
</div>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.BIO, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.BIO, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BIO, "", new { @class = "text-danger" })
</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>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<label class="label-success">@ViewBag.SuccessMessage</label>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<label class="label-warning">@ViewBag.AlreadyRegisteredMessage</label>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
模型类:
//-------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//-------------------------------------------------------------------------
namespace Trinity.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
public partial class tblUser
{
public int Id { get; set; }
[DisplayName("First Name")]
// [Required(ErrorMessage = "This field is required")]
[RegularExpression(@"^[^\W\d_]+$", ErrorMessage = "Only letters allowed in your name")]
public string FirstName { get; set; }
[DisplayName("Last Name")]
// [Required(ErrorMessage = "This field is required")]
[RegularExpression(@"^[^\W\d_]+$", ErrorMessage = "Only letters allowed in your name")]
public string LastName { get; set; }
public string BIO { get; set; }
[DisplayName("E-Mail")]
// [Required(ErrorMessage = "This field is required")]
[RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Incorrect E-mail Format")]
public string Email { get; set; }
[DataType(DataType.Password)]
// [Required(ErrorMessage = "This field is required")]
[RegularExpression(@"^.*(?=.{8,})(?=.*[\d])(?=.*[\W]).*$", ErrorMessage = "Password must be 8 characters, contain at least 1 digit and one special character")]
public string Password { get; set; }
[DisplayName("Confirm Password")]
[DataType(DataType.Password)]
// [Required(ErrorMessage = "This field is required")]
[Compare("Password", ErrorMessage ="Passwords are not the same")]
public string ConfirmPassword { get; set; }
public string Photo { get; set; }
[DisplayName("Age")]
// [Required(ErrorMessage = "This field is required")]
public int Age { get; set; }
public string Sex { get; set; }
public string Preferred { get; set; }
}
}