[英]is there a way on adding two models properties inside ASP.NET View
我已经尝试使这种情况发生了几个小时。我在模型文件夹中有两个模型,称为模型,我试图从模型中提取数据以在视图中显示它,我知道只有一个模型语句可以应用于视图。 因此,我创建了一个ViewModels,其中包含我想在视图中引用的属性。 现在,当我运行该应用程序时,我收到一个编译错误,内容为:
“编译器错误消息:CS0246:找不到类型或名称空间名称'Models(您是否缺少using指令或程序集引用?)”
如果有其他解决方法,请随时提出建议,我将不胜感激。
创建了新的ViewModel
public class MainModelscs <T> where T :class
{
public StoreAudit StoreAudit { get; set; }
public StoreQuestions StoreQuestions { get; set; }
public List<string> StoreWindow { get; set; }
public IPagedList<T> IndexList { get; set; }
}
我内部的ViewMode属性,
@model PopMarketing.ViewModel.MainModelscs<PopMarketing.Models>
模型1
public class StoreQuestions
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int ReviewId { get; set; }
public int AuditId { get; set; }
public int QuestionOne { get; set; }
public string QuestionTwo { get; set; }
public string QuestionThree { get; set; }
public string QuestionFour { get; set; }
}
模型2
[Key]
[Column(Order = 1)]
public int AuditId { get; set; }
public string Date { get; set; }
public int StoreNumber { get; set; }
public string StoreName { get; set; }
public string Address { get; set; }
public string Manager { get; set; }
控制器方式
public class AuditController : Controller
{
private PopMarketingContext db = new PopMarketingContext();
//
// GET: /Audit/
private const string PASSWORD = "MarchJava2016";
public ActionResult Index(string searchString)
{
int number;
int check;
bool result = Int32.TryParse(searchString, out number);
if (result)
{
check = Convert.ToInt32(searchString);
var shops = from s in db.StoreAudit
select s;
if (!String.IsNullOrEmpty(searchString))
{
shops = shops.Where(s => s.StoreName.ToUpper().Contains(searchString.ToUpper()) ||
s.StoreNumber.Equals(check));
}
return View(shops.ToList());
}
else
{
var shops = from s in db.StoreAudit
select s;
if (!String.IsNullOrEmpty(searchString))
{
shops = shops.Where(s => s.StoreName.ToUpper().Contains(searchString.ToUpper()));
}
return View(shops.ToList());
}
}
Jaimin是正确的,在@model中,您传递要用作模型的类,而不是该类的目录或名称空间。
至于正确使用视图模型:
假设我有两节课:
class Student
{
public string name;
public string mentor;
}
class Teacher
{
public string name;
public string salary;
}
我想让视图显示两个名字。 我可以像这样创建一个视图模型:
class StudentAndTeacherVM
{
public Teacher teacher;
public Student student;
}
控制器将如下所示:
public ActionResult Index()
{
var student = new Student();
*fill student*
var teacher = new Teacher();
*fill teacher*
var model = new PersonVM();
model.teacher = teacher;
model.student = student;
return view(model);
}
现在,您可以通过以下方式在视图中找到它们:
@ Model.student.name @ Model.teacher.name
或者,如果我想尽可能地提高效率,也可以将名称放入VM。
控制器:
public ActionResult Index()
{
var student = new Student();
*fill student*
var teacher = new Teacher();
*fill teacher*
var model = new PersonVM();
model.teacherName = teacher.name;
model.studentName= student.name;
return view(model);
}
视图:
class StudentAndTeacherVM
{
public string teacherName;
public string studentName;
}
在操作中创建MainModelscs实例
。 。 。
i.e. MainModelscs <string> Obj = new MainModelscs <string> ;
。 。 。
然后将其传递给视图。
return View(Obj);
现在您认为
@model PopMarketing.ViewModel.MainModelscs<string>
(将T作为您的模型名称而不是名称空间!)
对不起,我来晚了。 但是,这里是您如何联接两个表,将它们映射到模型并传递到视图的方法。 类的第一个,其属性与数据库的注释表相对应。
public class Comments
{
public int ID { get; set; }
public string CommentText { get; set; }
public string Username { get; set; }
public int Upvote { get; set; }
public int Downvote { get; set; }
public int InappropriateFlags { get; set; }
public int PostId { get; set; }
public string ControllerName { get; set; }
public int ReplyTo { get; set; }
public bool replied { get; set; }
}
现在,另一个用户配置文件表需要与注释表连接。
public class UserProfile
{
public int ID { get; set; }
public string Username { get; set; }
public string DisplayName { get; set; }
public int Reputation { get; set; }
public DateTime DateOfRegistration { get; set; }
public int Money { get; set; }
public string ImageUrl { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Country { get; set; }
public string State { get; set; }
public string City { get; set; }
public string Location { get; set; }
public int? PostalCode { get; set; }
public int ProfileViews { get; set; }
public string AboutMe { get; set; }
}
现在非常需要用于处理这两个表的联接的模型。 我没有包含两个表的所有属性,因为我在视图中不需要它们,但是您当然可以包含它们。 在这种情况下,请不要忘记也更新您的数据访问查询。
public class CommentUserProfileJoin
{
public int ID { get; set; }
public string CommentText { get; set; }
public string Username { get; set; }
public string DisplayName { get; set; }
public int Upvote { get; set; }
public int Downvote { get; set; }
public int InappropriateFlags { get; set; }
public int PostId { get; set; }
public string ControllerName { get; set; }
public int ReplyTo { get; set; }
public bool replied { get; set; }
public int UserProfileId { get; set; }
}
我正在使用精巧的ORM。 因此,这是创建联接并将其映射到上面编写的联接表的数据访问代码。
public IEnumerable<CommentUserProfileJoin > GetComments(int postId, string controllerName, int replyTo)
{
IEnumerable<CommentUserProfileJoin> comments;
const string query = "Select c.[id], c.[CommentText], c.[Username], c.[Upvote], c.[Downvote], c.[InappropriateFlags], c.[PostId], c.[ControllerName], c.[ReplyTo], c.[replied], u.[id] as UserProfileId, u.displayname from Main.Comments c LEFT JOIN Profile.UserProfiles u on c.username = u.username where c.PostId = @postId and c.ControllerName = @contName and c.ReplyTo = @ReplyTo order by ID desc";
comments = conObj.Query<CommentUserProfileJoin>(query, new { postId = postId, contName = controllerName, ReplyTo = replyTo });
return comments;
}
所以现在我有了两个表的联接模型,我可以在任何这样的视图中使用它
@model IEnumerable<DataAccessLayer.Models.CommentUserProfileJoin>
希望这对您有所帮助。 另外,我可能无意中没有遵循最佳实践。 如果有人可以通过评论通知他们,我会很高兴。 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.