|
|
|
ขอความช่วยเหลือ เรื่องการ validate ที่ต่างกัน แต่ต้องเรียก db ตัวเดียวกัน asp,net mvc ครับ |
|
|
|
|
|
|
|
ต้องการความช่วยเหลือครับ คือผมมี table Owner อยู่ แล้วทีนี้ว่าเวลา update กับ create จะ validate ต่างกัน แล้วคือผมจะต้องทำยังไงครับ คือเวลา update มันไม่รู้จัก database Owner อ่ะครับ จะต้องทำยังไงครับ เรียกว่า mapping model หรือเปล่า ขอ keyword ให้ผมไปค้นหาต่อก็ได้ครับ
ขอบคุณครับ
Model
Code (C#)
namespace WebPrinter.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
public class Owner
{
public int id { get; set; }
[Required]
[Display(Name="ชื่อ")]
public string firstname { get; set; }
[Required]
[Display(Name="นามสกุล")]
public string lastname { get; set; }
public Nullable<System.DateTime> createrdate { get; set; }
public string username { get; set; }
[DataType(DataType.Password)]
public string password { get; set; }
[Display(Name="Role")]
public string roles { get; set; }
[Display(Name="อีเมลล์")]
public string email { get; set; }
[Display(Name="ชื่อ - นามสกุล")]
public string name { get; set; }
public string HiddenPostBack { get; set; }
public IEnumerable<Owner> GetActiveMembers
{
get
{
PrinterStockEntities db = new PrinterStockEntities();
return (from x in db.Owners
select x).ToList();
}
}
public SelectList dp
{
get{
return new SelectList(new[]{
new{ ID = "", Name = "-- เลือก Role --"},
new{ ID = "Admin", Name = "Admin"},
new{ ID = "User", Name = "User"},
},
"ID", "Name", this.roles);
}
}
}
public class LogOnModel
{
public int id { get; set; }
[Required]
public string username { get; set; }
[Required]
[DataType(DataType.Password)]
public string password { get; set; }
}
public class EditUserModel
{
public int id { get; set; }
[Required]
[Display(Name = "ชื่อ")]
public string firstname { get; set; }
[Required]
[Display(Name = "นามสกุล")]
public string lastname { get; set; }
[Required]
[Display(Name = "Role")]
public string roles { get; set; }
[Display(Name = "อีเมลล์")]
public string email { get; set; }
}
public class CreateUserModel
{
public int id { get; set; }
[Required]
[Display(Name = "ชื่อ")]
public string firstname { get; set; }
[Required]
[Display(Name = "นามสกุล")]
public string lastname { get; set; }
[Required]
[Display(Name = "Role")]
public string roles { get; set; }
[Display(Name = "อีเมลล์")]
public string email { get; set; }
[Required]
public string username { get; set; }
[Required]
[DataType(DataType.Password)]
public string password { get; set; }
}
}
controller
Code (C#)
[HttpPost]
[Authorize(Roles = "Admin")]
public JsonResult Edit(EditUserModel owner)
{
if (ModelState.IsValid)
{
System.Diagnostics.Debug.WriteLine("xxx");
PrinterStockEntities db = new PrinterStockEntities();
db.Entry(owner).State = EntityState.Modified;
db.SaveChanges();
}
else
{
string errors = "";
foreach (ModelState modelState in ViewData.ModelState.Values)
{
foreach (ModelError error in modelState.Errors)
{
errors += error.ErrorMessage;
System.Diagnostics.Debug.WriteLine(error.ErrorMessage);
}
}
}
return Json("xxx");
}
Model.Context
Code (C#)
namespace WebPrinter.Models
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class PrinterStockEntities : DbContext
{
public PrinterStockEntities()
: base("name=PrinterStockEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<ColorUnit> ColorUnits { get; set; }
public virtual DbSet<Customer> Customers { get; set; }
public virtual DbSet<DetailDataPrinter> DetailDataPrinters { get; set; }
public virtual DbSet<Finishing> Finishings { get; set; }
public virtual DbSet<HeadDataPrinter> HeadDataPrinters { get; set; }
public virtual DbSet<Machine_Printer> Machine_Printer { get; set; }
public virtual DbSet<Owner> Owners { get; set; }
public virtual DbSet<Stock_Paper> Stock_Paper { get; set; }
public virtual DbSet<Sys_Print> Sys_Print { get; set; }
public virtual DbSet<Type_Finishing> Type_Finishing { get; set; }
public virtual DbSet<Unit> Units { get; set; }
public virtual DbSet<Job_Type> Job_Type { get; set; }
public virtual DbSet<Transfer> Transfers { get; set; }
public virtual DbSet<Province> Provinces { get; set; }
}
}
Tag : .NET, PostgreSQL, JavaScript, jQuery, Web (ASP.NET), C#
|
ประวัติการแก้ไข 2015-03-15 13:48:55
|
|
|
|
|
Date :
2015-03-15 13:45:24 |
By :
เด็กโง่ |
View :
1814 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|