tình hình là nhóm mình đang làm 1 project về bán hàng ol.
Mình đang làm phần form đăng ký thành viên, nhưng bị vướng mắc, các bạn giúp mình với:
đây là phần Models:
[SPOIL]
partial class Member
{
private int Status_Actived = 1;
eProjectEntitiesDataContext db = new eProjectEntitiesDataContext();
public bool IsActive()
{
if (this.Is_Active == Status_Actived)
return true;
else
return false;
}
public Member loadByEmail(string Email)
{
var List = from l in db.Members where l.Email == Email select l;
return List.First();
}
public bool LogOn(string Email, string Password)
{
var List = from l in db.Members where l.Email == Email && l.Password == Password select l;
if (List.Count() > 0)
{
return true;
}
else
{
return false;
}
}
}
public class RegisterModel
{
[Required(ErrorMessage = "Email is REQUIRED")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
public string Email { get; set; }
[Required(ErrorMessage = "Phone number is REQUIRED")]
[DataType(DataType.PhoneNumber)]
[Display(Name = "Phone number")]
public string Phone { get; set; }
[Required(ErrorMessage="Password is REQUIRED")]
[ValidatePasswordLength]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required(ErrorMessage = "Confirm Password is REQUIRED")]
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Required(ErrorMessage="Firstname is REQUIRED")]
[Display(Name= "Firstname")]
public string Firstname { get; set; }
[Required(ErrorMessage="Lastname is REQUIRED")]
[Display(Name= "Lastname")]
public string Lastname { get; set; }
[Required(ErrorMessage="District is REQUIRED")]
[Display(Name= "District")]
public string District { get; set; }
[Required(ErrorMessage="Street is REQUIRED")]
[Display(Name= "Street")]
public string Street { get; set; }
[Required(ErrorMessage="Gender is REQUIRED")]
[Display(Name= "Gender")]
public int Gender { get; set; }
}
[/SPOIL]
còn đây là phần view:
[SPOIL]
@using (Html.BeginForm()) {
@Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.")
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
@Html.LabelFor(m => m.Email)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Email)
@Html.ValidationMessageFor(m => m.Email)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.ConfirmPassword)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.ConfirmPassword)
@Html.ValidationMessageFor(m => m.ConfirmPassword)
</div>
<div class ="editor-field">
@Html.LabelFor(m => m.Phone)
</div>
<div class ="editor-field">
@Html.TextBoxFor(m => m.Phone)
@Html.ValidationMessageFor(m => m.Phone)
</div>
<div class ="editor-field">
@Html.LabelFor(m => m.Firstname)
</div>
<div class ="editor-field">
@Html.TextBoxFor(m => m.Firstname)
@Html.ValidationMessageFor(m => m.Firstname)
</div>
<div class ="editor-field">
@Html.LabelFor(m => m.Lastname)
</div>
<div class ="editor-field">
@Html.TextBoxFor(m => m.Lastname)
@Html.ValidationMessageFor(m => m.Lastname)
</div>
<div class ="editor-field">
@Html.LabelFor(m => m.District)
</div>
<div class ="editor-field">
@Html.TextBoxFor(m => m.District)
@Html.ValidationMessageFor(m => m.District)
</div>
<div class ="editor-field">
@Html.LabelFor(m => m.Street)
</div>
<div class ="editor-field">
@Html.TextBoxFor(m => m.Street)
@Html.ValidationMessageFor(m => m.Street)
</div>
<div class ="editor-field">
@Html.LabelFor(m => m.Gender)
</div>
<div class ="editor-field">
@Html.RadioButtonFor(m => m.Gender,1,1) Male
@Html.RadioButtonFor(m => m.Gender,0,0) Female
@Html.ValidationMessageFor(m => m.Gender)
</div>
<p>
<input type="submit" value="Register" class="button" />
<input type="reset" value="Reset" class="button" />
</p>
</fieldset>
</div>
}
[/SPOIL]
còn phần controller thì mình ko biết làm thế nào để add thêm dư liệu về thành viên mới vào database. tạm thời mình đang viết code như sau:
[SPOIL]
public ActionResult Register() {
//ViewModel.PasswordLength = MemberService.MinPasswordLength;
var v = ViewData.Model= db.Members.ToList();
var Member = new Member();
return View(Member);
}
//
// POST: /Account/Register
[HttpPost]
public ActionResult Register(RegisterModel RegisterModel)
{
try {
if(ModelState.IsValid){
db.Members.InsertOnSubmit(RegisterModel); // báo lỗi ở đây, members nó ko nhận RegisterModel
db.SubmitChanges();
return RedirectToAction("Index","Members");
}
}
catch {
return View();
}
}
[/SPOIL]
báo lỗi ở đây: "db.Members.InsertOnSubmit(RegisterModel)" Members nó ko nhận RegisterModel :(
Mình đang làm phần form đăng ký thành viên, nhưng bị vướng mắc, các bạn giúp mình với:
đây là phần Models:
[SPOIL]
partial class Member
{
private int Status_Actived = 1;
eProjectEntitiesDataContext db = new eProjectEntitiesDataContext();
public bool IsActive()
{
if (this.Is_Active == Status_Actived)
return true;
else
return false;
}
public Member loadByEmail(string Email)
{
var List = from l in db.Members where l.Email == Email select l;
return List.First();
}
public bool LogOn(string Email, string Password)
{
var List = from l in db.Members where l.Email == Email && l.Password == Password select l;
if (List.Count() > 0)
{
return true;
}
else
{
return false;
}
}
}
public class RegisterModel
{
[Required(ErrorMessage = "Email is REQUIRED")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
public string Email { get; set; }
[Required(ErrorMessage = "Phone number is REQUIRED")]
[DataType(DataType.PhoneNumber)]
[Display(Name = "Phone number")]
public string Phone { get; set; }
[Required(ErrorMessage="Password is REQUIRED")]
[ValidatePasswordLength]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required(ErrorMessage = "Confirm Password is REQUIRED")]
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Required(ErrorMessage="Firstname is REQUIRED")]
[Display(Name= "Firstname")]
public string Firstname { get; set; }
[Required(ErrorMessage="Lastname is REQUIRED")]
[Display(Name= "Lastname")]
public string Lastname { get; set; }
[Required(ErrorMessage="District is REQUIRED")]
[Display(Name= "District")]
public string District { get; set; }
[Required(ErrorMessage="Street is REQUIRED")]
[Display(Name= "Street")]
public string Street { get; set; }
[Required(ErrorMessage="Gender is REQUIRED")]
[Display(Name= "Gender")]
public int Gender { get; set; }
}
[/SPOIL]
còn đây là phần view:
[SPOIL]
@using (Html.BeginForm()) {
@Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.")
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
@Html.LabelFor(m => m.Email)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Email)
@Html.ValidationMessageFor(m => m.Email)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.ConfirmPassword)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.ConfirmPassword)
@Html.ValidationMessageFor(m => m.ConfirmPassword)
</div>
<div class ="editor-field">
@Html.LabelFor(m => m.Phone)
</div>
<div class ="editor-field">
@Html.TextBoxFor(m => m.Phone)
@Html.ValidationMessageFor(m => m.Phone)
</div>
<div class ="editor-field">
@Html.LabelFor(m => m.Firstname)
</div>
<div class ="editor-field">
@Html.TextBoxFor(m => m.Firstname)
@Html.ValidationMessageFor(m => m.Firstname)
</div>
<div class ="editor-field">
@Html.LabelFor(m => m.Lastname)
</div>
<div class ="editor-field">
@Html.TextBoxFor(m => m.Lastname)
@Html.ValidationMessageFor(m => m.Lastname)
</div>
<div class ="editor-field">
@Html.LabelFor(m => m.District)
</div>
<div class ="editor-field">
@Html.TextBoxFor(m => m.District)
@Html.ValidationMessageFor(m => m.District)
</div>
<div class ="editor-field">
@Html.LabelFor(m => m.Street)
</div>
<div class ="editor-field">
@Html.TextBoxFor(m => m.Street)
@Html.ValidationMessageFor(m => m.Street)
</div>
<div class ="editor-field">
@Html.LabelFor(m => m.Gender)
</div>
<div class ="editor-field">
@Html.RadioButtonFor(m => m.Gender,1,1) Male
@Html.RadioButtonFor(m => m.Gender,0,0) Female
@Html.ValidationMessageFor(m => m.Gender)
</div>
<p>
<input type="submit" value="Register" class="button" />
<input type="reset" value="Reset" class="button" />
</p>
</fieldset>
</div>
}
[/SPOIL]
còn phần controller thì mình ko biết làm thế nào để add thêm dư liệu về thành viên mới vào database. tạm thời mình đang viết code như sau:
[SPOIL]
public ActionResult Register() {
//ViewModel.PasswordLength = MemberService.MinPasswordLength;
var v = ViewData.Model= db.Members.ToList();
var Member = new Member();
return View(Member);
}
//
// POST: /Account/Register
[HttpPost]
public ActionResult Register(RegisterModel RegisterModel)
{
try {
if(ModelState.IsValid){
db.Members.InsertOnSubmit(RegisterModel); // báo lỗi ở đây, members nó ko nhận RegisterModel
db.SubmitChanges();
return RedirectToAction("Index","Members");
}
}
catch {
return View();
}
}
[/SPOIL]
báo lỗi ở đây: "db.Members.InsertOnSubmit(RegisterModel)" Members nó ko nhận RegisterModel :(