View Full Version : صدازدن تابع درون یک کلاس در mvc4
نیکناز
جمعه 28 تیر 1392, 00:35 صبح
سلام
من تو mvc4 در مدل ، یه کلاس را به صورت پابلیک و استاتیک تعریف میکنم با یه سری توابع درونش .
ولی وقتی در view می خوام از این کلاس استفاده کنم ، کلاس رو می شناسه اما توابع را لیست نمی کنه.
در صورتی که من این کار رو خیلی راحت در mvc3 به همین شکل انجام می دادم.
مرسی
mze666
جمعه 28 تیر 1392, 03:52 صبح
کد اون کلاس static رو هم قرار بدید تا بفهمیم مشکل از کجاست
نیکناز
جمعه 28 تیر 1392, 10:04 صبح
یه مدل به نام CentricModels.cs دارم . که توش کلاس User رو به صورت زیر تعریف کردم . و توی این کلاس تابع creat رو قرار دادم .
[public class User
{
LinqClassDataContext dc = new LinqClassDataContext();
//Project_Users
#region Users
public tbl_User Create(string userName
, string password, string mailAddress
, bool active, string job, string Mobile)
{
try
{
if (dc.tbl_Users.Where(m => m.UserName.ToLower() == userName.ToLower()).Count() == 0)
{
tbl_User user = new tbl_User
{
Active = active
,
Email = mailAddress,
Mobile = Mobile,
Job = "ناشر"
,
Password = password,
UserName = userName
};
dc.tbl_Users.InsertOnSubmit(user);
dc.SubmitChanges();
return user;
}
else
return null;
}
catch
{
return null;
}
}
public tbl_User Createusers(string userName
, string password, string mailAddress
, bool active, string job, string Mobile)
{
try
{
if (dc.tbl_Users.Where(m => m.UserName.ToLower() == userName.ToLower()).Count() == 0)
{
tbl_User user = new tbl_User
{
Active = active
,
Email = mailAddress,
Mobile = Mobile,
Job = job
,
Password = password,
UserName = userName
};
dc.tbl_Users.InsertOnSubmit(user);
dc.SubmitChanges();
return user;
}
else
return null;
}
catch
{
return null;
}
}
public bool Remove(tbl_User user)
{
try
{
//Delete All Relashions
dc.tbl_Users.DeleteOnSubmit(user);
dc.SubmitChanges();
return true;
}
catch
{
return false;
}
}
public bool Remove(int userID)
{
try
{
tbl_User user = dc.tbl_Users.First(p => p.ID == userID);
return Remove(user);
}
catch
{
return false;
}
}
public bool Remove(IEnumerable<tbl_User> users)
{
try
{
foreach (tbl_User user in users)
{
Remove(user);
}
return true;
}
catch
{
return false;
}
}
public tbl_User EditUser(int userID, string Email, string password, string Mobile, bool active)
{
try
{
tbl_User user = UserByID(userID);
user.Email = Email;
user.Password = password;
user.Mobile = Mobile;
user.Active = active;
dc.SubmitChanges();
return user;
}
catch
{
return null;
}
}
public tbl_User UserByID(int id)
{
try
{
return dc.tbl_Users.First(p => p.ID == id);
}
catch
{
return null;
}
}
public tbl_User UserByName(string userName)
{
try
{
return dc.tbl_Users.First(p => p.UserName == userName);
}
catch
{
return null;
}
}
public tbl_User UserByNiceName(string UserName)
{
try
{
if (UserName[UserName.Length - 1].ToString() == ";")
{
UserName = UserName.Substring(0, UserName.Length - 1);
}
if (UserName.Substring(0, 1) == ";")
{
UserName = UserName.Substring(1, UserName.Length - 1);
}
return dc.tbl_Users.First(p => p.UserName == UserName);
}
catch
{
return null;
}
}
public string UserUserNameByID(int id)
{
return dc.tbl_Users.First(p => p.ID == id).UserName;
}
public bool ChangeUserPassword(string newPassword, int userID)
{
try
{
tbl_User user = UserByID(userID);
user.Password = newPassword;
dc.SubmitChanges();
return true;
}
catch
{
return false;
}
}
public bool ChangeUserActive(int userID, bool active)
{
try
{
tbl_User user = UserByID(userID);
user.Active = active;
dc.SubmitChanges();
return true;
}
catch
{
return false;
}
}
public bool ChangeUserProfile(int userID, string UserName, string mailAddress)
{
try
{
tbl_User user = UserByID(userID);
user.UserName = UserName;
user.Email = mailAddress;
dc.SubmitChanges();
return true;
}
catch
{
return false;
}
}
public IEnumerable<tbl_User> AllUsers()
{
try
{
return dc.tbl_Users.Where(p => true);
}
catch
{
return null;
}
}
#endregion Users
}
]
حالا تو یه مدل دیگه مثلا AccountModels از کد زیر استفاده می کنم :
[public MembershipCreateStatus CreateUser(string userName, string password, string Mobile, string mailAddress, string job)
{
Centric cm = new Centric();
if (cm.Users.Create(userName, password, mailAddress, true, job, Mobile) != null)
{
return MembershipCreateStatus.Success;
}
else
{
return MembershipCreateStatus.InvalidUserName;
}
}
]
و تابع create بعد ز کلاس Users ، توقسمت if شناخته میشه . ولی در mvc4 لیست تابع ها اصلا نمی یاد.
نیکناز
جمعه 28 تیر 1392, 10:07 صبح
if (cm.Users.Create(userName, password, mailAddress, true, job, Mobile) != null)
نیکناز
جمعه 28 تیر 1392, 10:09 صبح
ببخشید من کدا رو می زارم تو تگ کد .
public class User
{
LinqClassDataContext dc = new LinqClassDataContext();
//Project_Users
#region Users
public tbl_User Create(string userName
, string password, string mailAddress
, bool active, string job, string Mobile)
{
try
{
if (dc.tbl_Users.Where(m => m.UserName.ToLower() == userName.ToLower()).Count() == 0)
{
tbl_User user = new tbl_User
{
Active = active
,
Email = mailAddress,
Mobile = Mobile,
Job = "ناشر"
,
Password = password,
UserName = userName
};
dc.tbl_Users.InsertOnSubmit(user);
dc.SubmitChanges();
return user;
}
else
return null;
}
catch
{
return null;
}
}
public tbl_User Createusers(string userName
, string password, string mailAddress
, bool active, string job, string Mobile)
{
try
{
if (dc.tbl_Users.Where(m => m.UserName.ToLower() == userName.ToLower()).Count() == 0)
{
tbl_User user = new tbl_User
{
Active = active
,
Email = mailAddress,
Mobile = Mobile,
Job = job
,
Password = password,
UserName = userName
};
dc.tbl_Users.InsertOnSubmit(user);
dc.SubmitChanges();
return user;
}
else
return null;
}
catch
{
return null;
}
}
public bool Remove(tbl_User user)
{
try
{
//Delete All Relashions
dc.tbl_Users.DeleteOnSubmit(user);
dc.SubmitChanges();
return true;
}
catch
{
return false;
}
}
public bool Remove(int userID)
{
try
{
tbl_User user = dc.tbl_Users.First(p => p.ID == userID);
return Remove(user);
}
catch
{
return false;
}
}
public bool Remove(IEnumerable<tbl_User> users)
{
try
{
foreach (tbl_User user in users)
{
Remove(user);
}
return true;
}
catch
{
return false;
}
}
public tbl_User EditUser(int userID, string Email, string password, string Mobile, bool active)
{
try
{
tbl_User user = UserByID(userID);
user.Email = Email;
user.Password = password;
user.Mobile = Mobile;
user.Active = active;
dc.SubmitChanges();
return user;
}
catch
{
return null;
}
}
public tbl_User UserByID(int id)
{
try
{
return dc.tbl_Users.First(p => p.ID == id);
}
catch
{
return null;
}
}
public tbl_User UserByName(string userName)
{
try
{
return dc.tbl_Users.First(p => p.UserName == userName);
}
catch
{
return null;
}
}
public tbl_User UserByNiceName(string UserName)
{
try
{
if (UserName[UserName.Length - 1].ToString() == ";")
{
UserName = UserName.Substring(0, UserName.Length - 1);
}
if (UserName.Substring(0, 1) == ";")
{
UserName = UserName.Substring(1, UserName.Length - 1);
}
return dc.tbl_Users.First(p => p.UserName == UserName);
}
catch
{
return null;
}
}
public string UserUserNameByID(int id)
{
return dc.tbl_Users.First(p => p.ID == id).UserName;
}
public bool ChangeUserPassword(string newPassword, int userID)
{
try
{
tbl_User user = UserByID(userID);
user.Password = newPassword;
dc.SubmitChanges();
return true;
}
catch
{
return false;
}
}
public bool ChangeUserActive(int userID, bool active)
{
try
{
tbl_User user = UserByID(userID);
user.Active = active;
dc.SubmitChanges();
return true;
}
catch
{
return false;
}
}
public bool ChangeUserProfile(int userID, string UserName, string mailAddress)
{
try
{
tbl_User user = UserByID(userID);
user.UserName = UserName;
user.Email = mailAddress;
dc.SubmitChanges();
return true;
}
catch
{
return false;
}
}
public IEnumerable<tbl_User> AllUsers()
{
try
{
return dc.tbl_Users.Where(p => true);
}
catch
{
return null;
}
}
#endregion Users
}
public MembershipCreateStatus CreateUser(string userName, string password, string Mobile, string mailAddress, string job)
{
Centric cm = new Centric();
if (cm.Users.Create(userName, password, mailAddress, true, job, Mobile) != null)
{
return MembershipCreateStatus.Success;
}
else
{
return MembershipCreateStatus.InvalidUserName;
}
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.