我的MVC3 vb.net应用程序出现问题。当我尝试将我所做的更改发布到控制器时,模型不会发送到控制器。
我曾尝试关注许多帖子,例如this one和this one,但我不确定如何在我的应用程序中实现它们,因为我的模型没有发送IEnumerable类型。
最后,我只希望模型为每个批次返回一个值,该值是我将保存到数据库的值。
当我发布模型并尝试发送到控制器时,页面通过post发送以下内容:
Client=2&Datacenters=20&BatchesForAssignation[0].CenterID=4&BatchesForAssignation[1].CenterID=20&BatchesForAssignation[1].DatacenterID=14...
但我不知道如何将此查询字符串转换为 BatchesForAssignation 对象,将其分配给模型并发送给控制器。
注意:查询字符串中显示的客户端和数据中心的值不会在控制器中使用。我需要 BatchesForAssignation [n] .CenterID 部分。
你能指点我找到解决方案吗?
这是我在MVC应用程序中使用的对象(代码压缩):
批处理:
Public class Batch
public property ID as integer
public property CenterID as integer
public property name as string
end class
中心(此对象只存储将分配给批次的所有中心列表。名称只是为了在下拉列表中显示名称):
Public class Center
public property ID as integer
public property name as string
end class
(还有一个Batchlist和一个Centerlist对象,它们作为从CollectionBase继承的集合,存储所有Batch和Center对象。如果你需要类定义,请告诉我,但非常明确。)
模型如下
Public class ProcessingModel
public property BatchesForAssignation as BatchList
public property Datacenters as CenterList
End class
控制器如下:
<HttpGet()>
<Authorize()> _
Public Function AssignToDataCenters() As ActionResult
Dim model As New ProcessingModel
Dim BatchHandler As New BatchControl
'This line will get the list of batches without datacenter
model.BatchesForAssignation = BatchHandler.GetBatchesAvailable(ConnectionString)
'This method will get the list of Datacenters available
model.Datacenters=BatchHandler.GetDatacenters(ConnectionString)
return View(model)
End Function
HttpPost(实际上这不起作用,因为模型返回一个空模型):
<HttpPost()>
<Authorize()> _
Public Function AssignToDataCenters(ByVal Model as ProcessingModel) As ActionResult
Dim BatchHandler As New BatchControl
Dim SaveResult as Boolean=false
'This line will get the list of batches without datacenter
model.BatchesForAssignation = BatchHandler.GetBatchesAvailable(ConnectionString)
'This method save the information returned by the model
SaveResult=BatchHandler.UpdateBatches(model)
ViewBag("Result")=SaveResult
Return View(model)
End Function
视图如下(是强类型视图):
@ModelType MVCAdmin.ProcessingModel
@Code
ViewData("Title") = "Assign Batches To centers"
End Code
@Using Html.BeginForm()
@<table id="tblAvailableBatches">
<thead>
<tr>
<th>Assign batch to:</th>
<th>Name</th>
</tr>
</thead>
<tbody>
@code
For i As Integer = 0 To Model.BatchesForAssignation.Count - 1
Dim a As Integer = i
@<tr>
<td>@Html.DropDownListFor(Function(m) m.BatchesForAssignation(a).CenterID, New SelectList(Model.Datacenters, "ID", "name", model.BatchesForAssignation(i).CenterID), " ")</td>
<td>@Model.BatchesForAssignation(i).name</td>
</tr>
Next
End Code
</tbody>
</table>
<input type="button" value="Apply changes" id="btnApply" />
End Using
提前致谢
更新2012-06-14:
在做了一些研究之后,我发现我可以使用request.Form
解析控制器中的查询字符串。我可以解析视图发送的结果。但是查询字符串的格式为BatchesForAssignation[0].CenterID
,BatchesForAssignation[1].CenterID
,BatchesForAssignation[2].CenterID
等等......
是否有更好的方法“自动”执行此操作,以便模型解析查询字符串并将解析后的对象发送到控制器?
再次......提前致谢
答案 0 :(得分:1)
在审核this question后,我发现创建模型并将其发送到控制器的最佳方法是创建CustomModelBinder
(来自IModelBinder
接口)并解析表单使用BindModel
属性在controllerContext.HttpContext.Request.Form
方法上查询字符串。像这样:
Public Class ProcessingModelBinder
implements IModelBinder
public Function BindModel(controllerContext As System.Web.Mvc.ControllerContext, bindingContext As System.Web.Mvc.ModelBindingContext) As Object
dim model as ProcessingModel = if(nothing.equals(bindingContext.Model),directcast(bindingContext.Model,directcast(DependencyResolver.Current.GetService(typeof(ProcessingModel))
dim Keys as string()=controllerContext.HttpContext.Request.Form.AllKeys
for each key in Keys
'Fill the model required parameters
Next
return model
End Function
最后在global.asax文件中注册新的模型构建器
Sub Application_Start()
AreaRegistration.RegisterAllAreas()
ModelBinders.Binders.Add(typeof(ProcessingModel),New ProcessingModelBinder())
RegisterGlobalFilters(GlobalFilters.Filters)
RegisterRoutes(RouteTable.Routes)
End Sub
我希望这有助于某人
此致