gpt4 book ai didi

asp.net - 为 ViewModel 创建 View 帖子返回具有空外键属性的 ViewModel

转载 作者:行者123 更新时间:2023-12-03 10:49:18 27 4
gpt4 key购买 nike

我正在用 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

流程数据集: (属性连接表)
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

我的目标是从数据集的索引或详细信息 View 中选择 1 个或多个数据集(使用域模型),然后单击指向 Add New Process to Dataset(s) 的链接这将打开这个所需的创建 View 。 Create View 创建的任何 ProcessDatasets 将自动引用选定的 Datasets,而无需用户从列表中选择它们。

下面是我为 ProcessDataset 创建的 ViewModel。

进程状态数据集:
Public Class ProcessStatusDataset

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

End Class

ProcessDatasetController 中的 Controller 方法:
'
' 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 View :
@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传递到 CreateProcessStatus POST 方法总是带有空白 Guid,其中应该有 Process 的 ID和 Owner . Datasets属性也是 Nothing .

当我查看渲染页面的来源时,我看到:
<form action="/ProcessDataset/CreateProcessStatus/e29bc119-b8c2-4ac5-9ce7-c9780673c193" method="post">

链接末尾的 Guid 是单个 Dataset 的 ID在 Dataset 中选择细节 View 。

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

最佳答案

下拉列表

我认为这行不通:

...

@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并检查 key 以检查您从帖子中带回了什么。

数据集问题

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

关于asp.net - 为 ViewModel 创建 View 帖子返回具有空外键属性的 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11365991/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com