1

我正在用 ASP.NET MVC3(我的第一个)开发一个 Web 应用程序,以跟踪我工作的工程公司的数据处理步骤。

以下是一些领域模型:

数据集:

Partial Public MustInherit Class Dataset
    Public Property DatasetID As System.Guid
    Public Property Name As String

    Public Overridable Property ProcessDatasets As ICollection(Of ProcessDataset) = New HashSet(Of ProcessDataset)
    Public Overridable Property DeliveryBatches As ICollection(Of DeliveryBatch) = New HashSet(Of DeliveryBatch)

End Class

过程:

Partial Public Class Process
    Public Property ProcessID As System.Guid
    Public Property Name As String
    Public Property Type As String
    Public Property Description As String
    Public Property SOP As String
    Public Property ProcessOrder As Nullable(Of Integer)

    Public Overridable Property ProcessDatasets As ICollection(Of ProcessDataset) = New HashSet(Of ProcessDataset)

End Class

ProcessDataset:(属性连接表)

Partial Public Class ProcessDataset
    Public Property ProcessID As System.Guid
    Public Property DatasetID As System.Guid
    Public Property OwnerID As Nullable(Of System.Guid)
    Public Property Started As Nullable(Of Date)
    Public Property Completed As Nullable(Of Date)

    Public Overridable Property Dataset As Dataset
    Public Overridable Property Process As Process
    Public Overridable Property ProcessOwner As ProcessOwner

End Class

我的目标是从 Dataset 的 Index 或 Details 视图中选择 1 个或多个 Datasets(使用域模型),然后单击一个链接,Add New Process to Dataset(s)该链接将打开这个所需的 Create 视图。Create 视图创建的任何 ProcessDatasets 将自动引用选定的 Datasets,而无需用户从列表中选择它们。

下面是我为 ProcessDataset 创建的 ViewModel。

过程状态数据集:

Public Class ProcessStatusDataset

    Public Property ProcessDataset As ProcessDataset
    Public Property Datasets As IEnumerable(Of Dataset)

End Class

ProcessDatasetController 中的控制器方法:

'
' GET: /ProcessDataset/Create

Function CreateProcessStatus(id As Guid) As ViewResult
    Dim processStatus As New ProcessStatusDataset
    processStatus.Datasets = db.Datasets.Where(Function(d) d.DatasetID = id)
    ViewBag.ProcessID = New SelectList(db.Processes, "ProcessID", "Name")
    ViewBag.OwnerID = New SelectList(db.ProcessOwners, "ProcessOwnerID", "Name")
    Return View(processStatus)
End Function

'
' POST: /ProcessDataset/Create

<HttpPost()>
Function CreateProcessStatus(processstatusdataset As ProcessStatusDataset) As ActionResult
    If ModelState.IsValid Then
        For Each dataset In processstatusdataset.Datasets
            Dim processdataset As New ProcessDataset
            processdataset.ProcessID = processstatusdataset.ProcessDataset.ProcessID
            processdataset.OwnerID = processstatusdataset.ProcessDataset.OwnerID
            processdataset.Completed = processstatusdataset.ProcessDataset.Completed
            processdataset.Started = processstatusdataset.ProcessDataset.Started
            processdataset.DatasetID = dataset.DatasetID
            db.ProcessDatasets.Add(processdataset)
        Next
        db.SaveChanges()
        Return RedirectToAction("Index")
    End If

    ViewBag.ProcessID = New SelectList(db.Processes, "ProcessID", "Name", processstatusdataset.ProcessDataset.ProcessID)
    ViewBag.OwnerID = New SelectList(db.ProcessOwners, "ProcessOwnerID", "Name", processstatusdataset.ProcessDataset.OwnerID)
    Return View(processstatusdataset)

CreateProcessStatus 视图:

@ModelType ProductionDataTrackingMVC.ProcessStatusDataset
@Code
    ViewData("Title") = "Add New Process to Datasets"
End Code
<h2>
    Add New Process to Datasets</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@Using Html.BeginForm("CreateProcessStatus","ProcessDataset")
    @Html.ValidationSummary(True)
    @<fieldset>
        <legend>Process Status</legend>
        <div class="editor-label">
            @Html.LabelForPascalCase(Function(model) model.ProcessDataset.Process):
        </div>
        <div class="editor-field">
            @Html.DropDownList("ProcessID", String.Empty)
            @Html.ValidationMessageFor(Function(model) model.ProcessDataset.ProcessID)
        </div>
        <div class="editor-label">
            @Html.LabelForPascalCase(Function(model) model.ProcessDataset.ProcessOwner):
        </div>
        <div class="editor-field">
            @Html.DropDownList("OwnerID", String.Empty)
            @Html.ValidationMessageFor(Function(model) model.ProcessDataset.OwnerID)
        </div>
        <div class="editor-label">
            @Html.LabelForPascalCase(Function(model) model.ProcessDataset.Started):
        </div>
        <div class="editor-field">
            @Html.EditorFor(Function(model) model.ProcessDataset.Started)
            @Html.ValidationMessageFor(Function(model) model.ProcessDataset.Started)
        </div>
        <div class="editor-label">
            @Html.LabelForPascalCase(Function(model) model.ProcessDataset.Completed):
        </div>
        <div class="editor-field">
            @Html.EditorFor(Function(model) model.ProcessDataset.Completed)
            @Html.ValidationMessageFor(Function(model) model.ProcessDataset.Completed)
        </div>
        <br />
        <div>
            <table>
                <tr>
                    <th>
                        Dataset Type
                    </th>
                    <th>
                        @Html.LabelForPascalCase(Function(model) model.Datasets.FirstOrDefault().Name)
                    </th>
                </tr>
    @For Each item In Model.Datasets
        Dim currentItem = item
                @<tr>
                    <td>
                        @currentItem.GetType().BaseType.Name
                    </td>
                    <td>
                        @Html.DisplayFor(Function(modelItem) currentItem.Name)
                    </td>
                </tr>
    Next
            </table>
        </div>
        <p>
            <input type="submit" value="Add New Process Status" />
        </p>
    </fieldset>
End Using
<div>
    @Html.ActionLink("Back to Process Status Datasets List", "Index")
</div>

此时,ProcessStatusDataset传入CreateProcessStatusPOST 方法的参数总是带有空白 Guid,其中应该有一个 IDProcessOwner。该Datasets物业也是Nothing

当我查看渲染页面的来源时,我看到:

<form action="/ProcessDataset/CreateProcessStatus/e29bc119-b8c2-4ac5-9ce7-c9780673c193" method="post">

链接末尾的 Guid 是Details 视图Dataset中所选单曲的 ID。Dataset

谁能指出我正确的方向。我一直在搜索谷歌,但没有运气。

4

1 回答 1

0

下拉菜单

我认为这行不通:

...

@Html.DropDownList("ProcessID", String.Empty)

...

相反,你应该尝试这样的事情:

...

@{
    Dim processes = DirectCast(ViewBag.Processes, SelectList);
}

@Html.DropDownListFor(Function(model) model.ProcessDataset.ProcessId, processes, "Select Process")

...

'remember to set this on the post too, in case of returning the same view.
ViewBag.Processes = New SelectList(db.Processes, "ProcessID", "Name")                   

...

这应该让模型绑定器(如果你不知道这个概念,你可能想用谷歌搜索)来发挥它的魔力。否则,只需将HttpPost操作方法​​的返回类型更改为FormCollection并检查键以检查您从帖子中返回的内容。

数据集问题

我认为您没有设置任何 html 表单字段来期望在您的帖子中看到某些内容。

于 2012-07-06T16:52:56.740 回答