我正在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
我的目标是从数据集的索引或详细信息视图中选择1个或多个数据集(使用域模型),然后单击指向Add New Process to Dataset(s)
的链接,这将打开此所需的创建视图。 Create视图创建的任何ProcessDatasets都将自动引用所选的数据集,而无需用户从列表中选择它们。
下面是我为ProcessDataset创建的ViewModel。
ProcessStatusDataset:
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
POST方法的CreateProcessStatus
始终带有空白Guid,其中应该有Process
和Owner
的ID。 Datasets
属性也是Nothing
。
当我查看呈现页面的来源时,我看到:
<form action="/ProcessDataset/CreateProcessStatus/e29bc119-b8c2-4ac5-9ce7-c9780673c193" method="post">
链接末尾的Guid是Dataset
详细信息视图中所选的单Dataset
的ID。
任何人都可以指出我正确的方向。我一直在寻找谷歌而没有运气。
答案 0 :(得分:0)
<强> Dropdowns的强>
我认为这不会起作用:
...
@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表单字段以期在您的帖子中看到某些内容。