如何将viewmodel与各种其他字段组合在一起

时间:2015-03-05 22:39:19

标签: asp.net-mvc asp.net-mvc-4

在我的mvc4应用程序中,我需要创建一个视图,客户可以从服务列表中进行选择,订阅(通过选择是/否选项)并提供他们完成服务的最后服务日期的详细信息,并提供提议的未来服务日期。它应该大致如下所示

enter image description here

我在数据库中有一个服务表,如服务(Id,Name等),但不知道如何将我显示的其他值(如yes/no)和两个日期组合在一个viewModel和传递它来查看和检索回发后的所有值。简单来说,我的viewmodel会有哪些字段?有任何想法吗。感谢

2 个答案:

答案 0 :(得分:1)

听起来你要求的不仅仅是视图模型。为了扩展shenku的答案,这将是我在VB中粗略/未经测试的方法。这绝不是包罗万象的,但希望能让您了解如何操作数据,将其传递给视图,以及在回发后获取数据。

模型/数据库对象:

Public Class Service
    Public Property ServiceID As Integer
    Public Property Name As String
End Class

Public Class CustomerService
    Public Property CustomerID As Integer
    Public Property ServiceID As Integer
    Public Property Selected As Boolean
    Public Property LastDate As DateTime
    Public Property ProposedDate As DateTime
End Class

视图模型:

Public Class ViewRow
    Public Property ServiceID As Integer
    Public Property Name As String
    Public Property YesSelected As Boolean
    Public Property NoSelected As Boolean
    Public Property LastDate As String
    Public Property ProposedDate As String
End Class

Public Class ViewModel
    Public Property TableHeaders As String() = {"Services","Yes","No","Date of Last Service", "Proposed Date"}
    Public Property ServiceDetails As List(Of ViewRow)
End Class

控制器:

Public Class HomeController
    Inherits System.Web.Mvc.Controller

    ' Simulating EntityFramework
    Protected db As New MyEntities

    Function ServiceList() As ActionResult

        Dim thisCustomerID As Integer
        ' *Set user's customer ID*

        ' Using a LINQ join to combine with other service information
        Dim vm As New ViewModel With {
            .ServiceDetails = ( _
                From custService In db.CustomerService().ToList()
                Join service In db.Service().ToList()
                On custService.ServiceID Equals service.ServiceID
                Where custService.CustomerID.Equals(thisCustomerID)
                Select New ViewRow With {
                    .ServiceID = service.ServiceID,
                    .Name = service.Name,
                    .YesSelected = custService.Selected,
                    .NoSelected = Not custService.Selected,
                    .LastDate = custService.LastDate.ToString("MMM yyyy"),
                    .ProposedDate = custService.ProposedDate.ToString("MMM yyyy")                  
                }).ToList()
            }

        ' Passing to a strongly-typed view of type "ViewModel"
        Return View("serviceList",model:=vm)
    End Function

    ' This is where you post back, and data can be bound to type "ViewModel"
    <HttpPost()> _
    Function ServiceList(data As ViewModel) As ActionResult
        ' *Model Validation / Insert / Update*

        ' Refresh the page (if you want)
        RedirectToAction("ServiceList","Home")
    End Function
End Class

Razor View(serviceList.vbhtml):

@ModelType ViewModel
<div>
    <table>
        <tr>
            @For Each head In Model.TableHeaders
                @<th>@(head)</th>
            Next
        </tr>
        @For Each detail In Model.ServiceDetails
            @<tr id=@(detail.ServiceID)>
                 <td>@(detail.Name)</td>
                 <td>@(If(detail.YesSelected,"X",""))</td>
                 <td>@(If(detail.NoSelected,"X",""))</td>
                 <td>@(detail.LastDate)</td>
                 <td>@(detail.ProposedDate)</td>
             </tr>
        Next
    </table>
</div>

要回发,你必须将javascript抓取数据输入到任何输入字段中(我这里没有包含任何内容),并使用适当的数据构造一个JSON对象 - 它反映了财务主任的岗位行动。我提供了一个类型为ViewModel的参数的示例。这意味着您的JSON字段必须与ViewModel模型中定义的JSON字段匹配,并且它们的值必须与相应属性的数据类型匹配。 ASP.NET将在回发后绑定数据。此外,ViewModel很复杂,因此您可以发布ViewRow列表(用于多个记录更新)。要绑定它,您的JSON对象需要具有ServiceDetails属性,该属性包含一个对象数组,这些对象又具有ServiceIDNameYesSelected等属性。 / p>

答案 1 :(得分:0)

viewmodel中的服务集合应该这样做,Selected bool当然会代表yes / no选项,并且可能绑定到一个复选框。

public class ViewModel 
{
    public IList<Service> Services {get;set;}
}

public class Service 
{
    public bool Selected {get;set;}
    public DateTime LastDate {get;set;}
    public DateTime ProposedDate {get;set;}
}