asp.net mvc 视图模型。它们应该包含多少逻辑(如果有)

发布于 2024-09-06 11:52:18 字数 2074 浏览 6 评论 0原文

我一直在研究 mvc 的视图模型,并且正在寻找实现它们的最佳方法。我读过很多不同的文章,但似乎没有一个是明确的“最佳方法”。到目前为止,我可能有一个具有以下属性的客户模型:

  • 名字
  • 姓氏
  • 标题
  • 位置

其中位置是数据库中位置表的外键。

我希望能够编辑该客户,但只能编辑名字、姓氏和位置。我不关心编辑中的标题。因此,在我看来,我需要传递一个客户和一个选定的列表。

现在,根据我所读到的内容,我有以下选项(可能还有更多)。

所以我的问题基本上是哪一个是最好的?

1)

ViewData["Location"] 添加一个选择列表,然后创建一个强类型的客户视图?

2)

创建一个视图模型,在其中传递客户和选择列表(数据访问在控制器中完成):

public class ViewModelTest
{
    public Customer Customer { get; set; }
    public SelectList Locations { get; set; }

    public ViewModelTest(Customer customer, SelectList locations)
    {
        Customer = customer;
        Locations = locations;
    }
}

3)

创建一个视图模型,在其中传递客户和位置列表,并在视图模型中创建选择列表。

public class ViewModelTest
{
    public Customer Customer { get; set; }
    public SelectList Locations { get; set; }

    public ViewModelTest(Customer customer, List<Location> locations, string selectedLocation)
    {
        Customer = customer;
        Locations = new SelectList(locations, "LocationID", "LocationName", selectedLocation);
    }
}

4)

传递客户和存储库并在视图模型中进行数据访问。

public class ViewModelTest
{
    public Customer Customer { get; set; }
    public SelectList Locations { get; set; }

    public ViewModelTest(Customer customer, IRepository repository, string selectedLocation)
    {
        Customer = customer;
        Locations = new SelectList(repository.GetLocations(), "LocationID", "LocationName", selectedLocation);
    }
}

5)

仅使用我需要的属性创建视图模型:

public class ViewModelTest
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public SelectList Locations { get; set; }

    public ViewModelTest(Customer customer, SelectList locations)
    {
        FirstName = customer.FirstName;
        LastName = customer.LastName ;
        Locations = locations;
    }
}

6)

或上述或其他方式的其他组合。

欢迎所有意见。

I've been looking into view models for mvc and I'm looking for the best way to do them. I've read loads of different articles but none seem to be clear as the "best way." So far example I might have a Customer model with the following properties:

  • First Name
  • Last Name
  • Title
  • Location

Where location is a foreign key to a location table in the database.

I want to be able to edit this customer but only the first name, last name and location. I'm not bothered about the title in the edit. So in my view I will need to pass a customer and a selected list.

Now from what I've read I have the following options (there's probably many more).

So my question is basically which is the best one?

1)

Add a select list to the ViewData["Location"] and just create a strongly typed view of customer?

2)

Create a view model where I pass a customer and select list (the data access is done in the controller):

public class ViewModelTest
{
    public Customer Customer { get; set; }
    public SelectList Locations { get; set; }

    public ViewModelTest(Customer customer, SelectList locations)
    {
        Customer = customer;
        Locations = locations;
    }
}

3)

Create a view model where I pass a customer and list of locations and create the select list in the view model.

public class ViewModelTest
{
    public Customer Customer { get; set; }
    public SelectList Locations { get; set; }

    public ViewModelTest(Customer customer, List<Location> locations, string selectedLocation)
    {
        Customer = customer;
        Locations = new SelectList(locations, "LocationID", "LocationName", selectedLocation);
    }
}

4)

Pass a customer and repository and do the data access in the view model.

public class ViewModelTest
{
    public Customer Customer { get; set; }
    public SelectList Locations { get; set; }

    public ViewModelTest(Customer customer, IRepository repository, string selectedLocation)
    {
        Customer = customer;
        Locations = new SelectList(repository.GetLocations(), "LocationID", "LocationName", selectedLocation);
    }
}

5)

Create the view model with just the properties I need:

public class ViewModelTest
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public SelectList Locations { get; set; }

    public ViewModelTest(Customer customer, SelectList locations)
    {
        FirstName = customer.FirstName;
        LastName = customer.LastName ;
        Locations = locations;
    }
}

6)

Or some other combination of the above or another way.

All opinions welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我做我的改变 2024-09-13 11:52:18

这是我的建议:拥有一个反映强类型视图字段的视图模型:

public class SomeViewModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Location { get; set; }
    public IEnumerable<SelectListItem> PossibleLocations { get; set; }
}

并在控制器操作中填充此视图模型:

public ActionResult Index()
{
    var customer = Repository.GetCustomer();
    var locations = Repository.GetLocations();
    var viewModel = new SomeViewModel
    {
        FirstName = customer.FirstName,
        LastName = customer.LastName,
        Location = customer.Location,
        PossibleLocations = new SelectList(locations, "LocationID", "LocationName", customer.Location);
    };
    return View(viewModel);
}

[HttpPost]
public ActionResult Index(SomeViewModel viewModel)
{
    // TODO: Handle the form submission
    return View(viewModel);
}

当然,如我的示例所示,手动进行模型和视图模型之间的映射可能会变得相当麻烦在这种情况下,我建议您查看 AutoMapper

Here's what I may suggest: have a view model which reflects the fields of strongly typed view:

public class SomeViewModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Location { get; set; }
    public IEnumerable<SelectListItem> PossibleLocations { get; set; }
}

And in your controller action populate this view model:

public ActionResult Index()
{
    var customer = Repository.GetCustomer();
    var locations = Repository.GetLocations();
    var viewModel = new SomeViewModel
    {
        FirstName = customer.FirstName,
        LastName = customer.LastName,
        Location = customer.Location,
        PossibleLocations = new SelectList(locations, "LocationID", "LocationName", customer.Location);
    };
    return View(viewModel);
}

[HttpPost]
public ActionResult Index(SomeViewModel viewModel)
{
    // TODO: Handle the form submission
    return View(viewModel);
}

Of course doing the mapping between the model and the view model manually as shown my example could become quite cumbersome and in this case I would recommend you looking at AutoMapper.

秋日私语 2024-09-13 11:52:18

我将我的 ViewModel 作为

public class SomeViewModel 
{ 
    public Customer Customer { get; set; } 
    public IEnumerable<Location> PossibleLocations { get; set; } 
} 

我的控制器,如下所示:

public ActionResult Index()   
{     
    var viewModel = new SomeViewModel   
    {   
        Customer = Repository.GetCustomer(),
        PossibleLocations = Repository.GetLocations()
    };   
    return View(viewModel);   
}

然后您可以在视图中访问 Customer 对象中的所有内容,如下所示:

Customer name - <%: Model.Customer.FirstName %> <%: Model.Customer.LastName %>
Location - <%: Html.DropDownList("LocationID", new SelectList(Model.PossibleLocations as IEnumerable, "LocationID", "LocationName", Model.Location.LocationID))%>

I'd have my ViewModel as this

public class SomeViewModel 
{ 
    public Customer Customer { get; set; } 
    public IEnumerable<Location> PossibleLocations { get; set; } 
} 

My controller like this:

public ActionResult Index()   
{     
    var viewModel = new SomeViewModel   
    {   
        Customer = Repository.GetCustomer(),
        PossibleLocations = Repository.GetLocations()
    };   
    return View(viewModel);   
}

and then you can access everything in your Customer object in the view like this:

Customer name - <%: Model.Customer.FirstName %> <%: Model.Customer.LastName %>
Location - <%: Html.DropDownList("LocationID", new SelectList(Model.PossibleLocations as IEnumerable, "LocationID", "LocationName", Model.Location.LocationID))%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文