Asp.net mvc 3 我是否可以基于ViewModel添加视图,而不为新添加的视图添加控制器?
我是MVC3的新手,我想知道这是否可能,是否是一个好的实践 我有一个模型+视图+控制器,运行良好。此视图显示人员列表-我希望能够单击一个人的姓名并重定向到显示该人员详细信息的新视图。这个新视图只有一个ViewModel,但没有控制器,因为我计划在动作中传入对象 Person对象包含我的视图需要显示的所有属性: @ActionLink(item.Person.FirstName,“PersonDetails”,item.Person)Asp.net mvc 3 我是否可以基于ViewModel添加视图,而不为新添加的视图添加控制器?,asp.net-mvc-3,Asp.net Mvc 3,我是MVC3的新手,我想知道这是否可能,是否是一个好的实践 我有一个模型+视图+控制器,运行良好。此视图显示人员列表-我希望能够单击一个人的姓名并重定向到显示该人员详细信息的新视图。这个新视图只有一个ViewModel,但没有控制器,因为我计划在动作中传入对象 Person对象包含我的视图需要显示的所有属性: @ActionLink(item.Person.FirstName,“PersonDetails”,item.Person) 这是否可能/良好的实践???我相信您对MVC的工作原理有误解。
这是否可能/良好的实践???我相信您对MVC的工作原理有误解。ActionLink将始终重定向到控制器的相应ActionMethod。您要做的是在控制器中创建一个动作方法,该方法接受必要的参数,然后返回到视图视图模型 下面是一个快速入门的示例:
public class HomeController : Controller
{
public ActionResult List()
{
return View();
}
public ActionResult DetailById(int i)
{
// load person from data source by id = i
// build PersonDetailViewModel from data returned
return View("PersonDetails", PersonDetailViewModel);
}
public ActionResult DetailByVals(string FirstName, Person person)
{
// build PersonDetailViewModel manually from data passed in
// you may have to work through some binding issues here with Person
return View("PersonDetails", PersonDetailViewModel);
}
}
我相信您对MVC的工作原理有误解。ActionLink将始终重定向到控制器的相应ActionMethod。您要做的是在控制器中创建一个动作方法,该方法接受必要的参数,然后返回到视图视图模型 下面是一个快速入门的示例:
public class HomeController : Controller
{
public ActionResult List()
{
return View();
}
public ActionResult DetailById(int i)
{
// load person from data source by id = i
// build PersonDetailViewModel from data returned
return View("PersonDetails", PersonDetailViewModel);
}
public ActionResult DetailByVals(string FirstName, Person person)
{
// build PersonDetailViewModel manually from data passed in
// you may have to work through some binding issues here with Person
return View("PersonDetails", PersonDetailViewModel);
}
}
这不是一个很好的方式来做你想(在你原来的帖子)。视图应始终具有视图模型。视图模型只表示您希望在视图中包含的数据,不多也不少。不要将域模型传递给视图,而是使用视图模型。此视图模型可能只包含域模型属性的一个portain 在列表视图中,您可能有一个网格,在每一行旁边,您可能有一个详细信息链接,或者名称上的链接(如您所拥有的)。单击这些链接中的任何一个后,您将被定向到详细信息视图。此详细信息视图将具有自己的视图模型,其中仅包含需要在详细信息视图上显示的属性 domail模型可能类似于:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string ExampleProperty1 { get; set; }
public string ExampleProperty2 { get; set; }
public string ExampleProperty3 { get; set; }
}
假设您只想显示此人的id、名字、姓氏和年龄,则视图模型如下所示:
public class PersonDetailsViewModel
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
public class PersonController : Controller
{
private readonly IPersonRepository personRepository;
public PersonController(IPersonRepository personRepository)
{
// Check that personRepository is not null
this.personRepository = personRepository;
}
public ActionResult Details(int id)
{
// Check that id is not 0 or less than 0
Person person = personRepository.GetById(id);
// Now that you have your person, do a mapping from domain model to view model
// I use AutoMapper for all my mappings
PersonDetailsViewModel viewModel = Mapper.Map<PersonDetailsViewModel>(person);
return View(viewModel);
}
}
您不需要ExampleProperty1、ExampleProperty2和ExampleProperty3,因为它们不是必需的
您的个人控制器可能如下所示:
public class PersonDetailsViewModel
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
public class PersonController : Controller
{
private readonly IPersonRepository personRepository;
public PersonController(IPersonRepository personRepository)
{
// Check that personRepository is not null
this.personRepository = personRepository;
}
public ActionResult Details(int id)
{
// Check that id is not 0 or less than 0
Person person = personRepository.GetById(id);
// Now that you have your person, do a mapping from domain model to view model
// I use AutoMapper for all my mappings
PersonDetailsViewModel viewModel = Mapper.Map<PersonDetailsViewModel>(person);
return View(viewModel);
}
}
公共类PersonController:Controller
{
私有只读IPersonRepository personRepository;
公共PersonController(IPersonRepository personRepository)
{
//检查personRepository是否不为null
this.personRepository=personRepository;
}
公共行动结果详细信息(int id)
{
//检查id是否不为0或小于0
Person=personRepository.GetById(id);
//现在您已经有了您的人员,请执行从域模型到视图模型的映射
//我对所有映射使用AutoMapper
PersonDetailsViewModel viewModel=Mapper.Map(person);
返回视图(viewModel);
}
}
我希望这能让事情变得更清楚一点。这不是一个按照你的意愿去做的好方法(在你原来的帖子中)。视图应始终具有视图模型。视图模型只表示您希望在视图中包含的数据,不多也不少。不要将域模型传递给视图,而是使用视图模型。此视图模型可能只包含域模型属性的一个portain 在列表视图中,您可能有一个网格,在每一行旁边,您可能有一个详细信息链接,或者名称上的链接(如您所拥有的)。单击这些链接中的任何一个后,您将被定向到详细信息视图。此详细信息视图将具有自己的视图模型,其中仅包含需要在详细信息视图上显示的属性 domail模型可能类似于:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string ExampleProperty1 { get; set; }
public string ExampleProperty2 { get; set; }
public string ExampleProperty3 { get; set; }
}
假设您只想显示此人的id、名字、姓氏和年龄,则视图模型如下所示:
public class PersonDetailsViewModel
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
public class PersonController : Controller
{
private readonly IPersonRepository personRepository;
public PersonController(IPersonRepository personRepository)
{
// Check that personRepository is not null
this.personRepository = personRepository;
}
public ActionResult Details(int id)
{
// Check that id is not 0 or less than 0
Person person = personRepository.GetById(id);
// Now that you have your person, do a mapping from domain model to view model
// I use AutoMapper for all my mappings
PersonDetailsViewModel viewModel = Mapper.Map<PersonDetailsViewModel>(person);
return View(viewModel);
}
}
您不需要ExampleProperty1、ExampleProperty2和ExampleProperty3,因为它们不是必需的
您的个人控制器可能如下所示:
public class PersonDetailsViewModel
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
public class PersonController : Controller
{
private readonly IPersonRepository personRepository;
public PersonController(IPersonRepository personRepository)
{
// Check that personRepository is not null
this.personRepository = personRepository;
}
public ActionResult Details(int id)
{
// Check that id is not 0 or less than 0
Person person = personRepository.GetById(id);
// Now that you have your person, do a mapping from domain model to view model
// I use AutoMapper for all my mappings
PersonDetailsViewModel viewModel = Mapper.Map<PersonDetailsViewModel>(person);
return View(viewModel);
}
}
公共类PersonController:Controller
{
私有只读IPersonRepository personRepository;
公共PersonController(IPersonRepository personRepository)
{
//检查personRepository是否不为null
this.personRepository=personRepository;
}
公共行动结果详细信息(int id)
{
//检查id是否不为0或小于0
Person=personRepository.GetById(id);
//现在您已经有了您的人员,请执行从域模型到视图模型的映射
//我对所有映射使用AutoMapper
PersonDetailsViewModel viewModel=Mapper.Map(person);
返回视图(viewModel);
}
}
我希望这能让事情更清楚一点。当您单击该人员详细信息链接时,您必须了解这将创建对web服务的GET请求。该web服务将评估您请求的路由,并将其与区域、控制器和操作匹配,同时绑定请求参数(GET或POST)。最后,您真正需要的是控制器的回答,而不是视图。控制器将请求查看,以便他可以呈现一个可爱的页面发送给您,但发送该页面的仍然是控制器。当您单击该人员详细信息链接时,您必须了解这将创建对web服务的GET请求。该web服务将评估您请求的路由,并将其与区域、控制器和操作匹配,同时绑定请求参数(GET或POST)。最后,您真正需要的是控制器的回答,而不是视图。控制器将请求一个视图,以便他可以呈现一个可爱的页面发送给您,但它仍然是C