httppost没有返回值与MVC3剃刀vb.net复杂的对象 [英] httppost returns nothing with complex objects in mvc3 razor vb.net

查看:137
本文介绍了httppost没有返回值与MVC3剃刀vb.net复杂的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的MVC3 vb.net应用程序问题。当我尝试后我已经到控制器所做的更改,该模型是不发送到控制器。

我试图按照很多帖子像<一个href=\"http://stackoverflow.com/questions/7074922/asp-net-mvc3-complex-class-not-being-passed-as-null-on-httppost-method\">this 之一和<一个href=\"http://stackoverflow.com/questions/6523131/mvc3-submit-returning-null-on-my-complex-data-type\">this 之一,但我不知道如何实现他们在我的应用程序,因为我的模型没有送IEnumerable的类型。

最后我只希望,该模型会返回一个值每批那就是我会保存到数据库中的值。

当我发布模式并尝试发送到控制器的页面通过邮局发送以下内容:

<$p$p><$c$c>Client=2&Datacenters=20&BatchesForAssignation[0].CenterID=4&BatchesForAssignation[1].CenterID=20&BatchesForAssignation[1].DatacenterID=14...

但我不知道如何将此查询字符串转换为 BatchesForAssignation 对象,将其分配给模型,并传送给控制器。

注:为值客户端数据中心的查询字符串表示未在控制器中使用。我需要在 BatchesForAssignation [N] .CenterID 部分。

您可以请点我发现了一个解决方案?

这是我用我的MVC应用程序(code压实)的对象:

批次:

 公共类批次
    公共属性ID作为整数
    公共财产CenterID为整数
    公共属性名作为字符串
末级

中心(该对象只是存储将被分配到批量中心的所有列表中的名字只是为了显示在下拉列表中的名称):

 公共类中心
    公共属性ID作为整数
    公共属性名作为字符串
末级

(还有一个Batchlist以及用作集合从CollectionBase中存储的所有批次和中心对象继承Centerlist的对象。如果你需要的类定义,请让我知道,但为pretty strightforward)。

模型如下:

 公共类ProcessingModel
    公共财产BatchesForAssignation为BatchList
    公共财产作为数据中心CenterList
末级

控制器如下:

 &LT; HTTPGET()&GT;
&LT;授权()&GT; _
公共职能AssignToDataCenters()为的ActionResult
    昏暗的模型作为新ProcessingModel
    昏暗BatchHandler作为新BatchControl    这条线将分批获得无数据中心名单
    model.BatchesForAssignation = BatchHandler.GetBatchesAvailable(的ConnectionString)    这种方法将获取可用数据中心的名单
    model.Datacenters = BatchHandler.GetDatacenters(的ConnectionString)
    返回查看(模型)
结束功能

HttpPost(这实际上是不工作,因为该模型返回一个空模型):

 &LT; HttpPost()&GT;
&LT;授权()&GT; _
公共职能AssignToDataCenters(BYVAL模型作为ProcessingModel)作为的ActionResult
    昏暗BatchHandler作为新BatchControl
    昏暗SaveResult布尔= FALSE    这条线将分批获得无数据中心名单
    model.BatchesForAssignation = BatchHandler.GetBatchesAvailable(的ConnectionString)    这种方法节省模式返回的信息
    SaveResult = BatchHandler.UpdateBatches(模型)    ViewBag(结果)= SaveResult
    返回查看(模型)
结束功能

视图如下(是一个强类型的视图):

  @ModelType MVCAdmin.ProcessingModel@ code
    ViewData的(标题)=分配分批中心
结束code@using Html.BeginForm()
    @&LT;表ID =tblAvailableBatches&GT;
        &LT;&THEAD GT;
            &所述; TR&GT;
                &所述;第i分配批次:或其可/第i
                &LT;第i个姓名和LT; /第i
            &LT; / TR&GT;
        &LT; / THEAD&GT;
        &LT;&TBODY GT;
            @ code
                对于我作为整数= 0〜Model.BatchesForAssignation.Count - 1
                    暗淡了作为整数= I
                    @&LT; TR&GT;
                        &所述; TD&GT; @ Html.DropDownListFor(功能(米)m.BatchesForAssignation(一).CenterID,新的SelectList(Model.Datacenters,ID,姓名,model.BatchesForAssignation(ⅰ).CenterID),) &LT; / TD&GT;
                        &LT; TD&GT; @ Model.BatchesForAssignation(I)。名称&LT; / TD&GT;
                    &LT; / TR&GT;
                下一个
            结束code
        &LT; / TBODY&GT;
    &LT; /表&gt;
    &LT;输入类型=按钮值=应用修改ID =btnApply/&GT;
使用完

在此先感谢

更新2012-06-14:

做一些researh我发现我可以用 REQUEST.FORM 我可以分析视图所发出的结果分析控制器查询字符串后。但查询字符串键的形式<$c$c>BatchesForAssignation[0].CenterID,<$c$c>BatchesForAssignation[1].CenterID,<$c$c>BatchesForAssignation[2].CenterID等等...

时有更好的方法来做到这一点自动的,因此,该模型解析查询字符串和解析的对象发送到控制器?

再次...在此先感谢


解决方案

在审查这个问题我发现,创建模型,并将其发送到控制器的最佳方式是创建一个 CustomModelBinder (从 IModelBinder 接口)和解析关于使用 controllerContext.HttpContext.Request.Form 属性 BindModel 法形式的查询字符串。事情是这样的:

 公共类ProcessingModelBinder
    实现IModelBinder    公共职能BindModel(controllerContext作为System.Web.Mvc.ControllerContext,为的BindingContext System.Web.Mvc.ModelBindingContext)作为对象
        昏暗的模型ProcessingModel = if(nothing.equals(bindingContext.Model),directcast(bindingContext.Model,directcast(DependencyResolver.Current.GetService(typeof(ProcessingModel))
        昏暗的钥匙串为()= controllerContext.HttpContext.Request.Form.AllKeys        在按键的每个键
            '填写所需的模型参数
        下一个        收益模型
结束功能

终于在Global.asax文件注册新的模型构建器

 子的Application_Start()
    AreaRegistration.RegisterAllAreas()
    ModelBinders.Binders.Add(typeof运算(ProcessingModel),新ProcessingModelBinder())
    RegisterGlobalFilters(GlobalFilters.Filters)
    的RegisterRoutes(RouteTable.Routes)
结束小组

我希望这可以帮助别人

问候

I'm having issues with my MVC3 vb.net application. When I try to post the changes I've made to the controller, the model is not send to the controller.

I've tried to follow many posts like this one and this one, but I'm not sure of how to implement them in my application since my model did not send IEnumerable types.

At the end I only want that the model returns one value for each batch that is the value that I will save to the database.

When I post the model and try to send to the controller the page sends the following by post:

Client=2&Datacenters=20&BatchesForAssignation[0].CenterID=4&BatchesForAssignation[1].CenterID=20&BatchesForAssignation[1].DatacenterID=14...

But I don't know how to convert this querystring to a BatchesForAssignation object, assign it to the model and send to the controller.

NOTE: The values for Client and Datacenters shown in the querystring are not used in the controller. I need the BatchesForAssignation[n].CenterID part.

Can you please point me to found a solution on this?

This are the objects that I'm using in my MVC application (code compacted):

Batch:

Public class Batch
    public property ID as integer
    public property CenterID as integer
    public property name as string
end class

Centers (This object just store all the list of centers that will be assigned to the Batch. The name is just to show the name in the drop down list):

Public class Center
    public property ID as integer
    public property name as string
end class

(There's also a Batchlist and a Centerlist objects that acts as collections inherited from CollectionBase that stores all the Batch and Center objects. If you need the class definition please let me know but is pretty strightforward).

The model is as follows

Public class ProcessingModel
    public property BatchesForAssignation as BatchList
    public property Datacenters as CenterList
End class

The Controller is as follows:

<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 (This is actually not working because the model returns an empty model):

<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

The View is as follows (is a Strongly-typed view):

@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

Thanks in advance

UPDATE 2012-06-14:

After making some researh I found that I can parse the querystring in the controller using request.Form I can parse the results sent by the view. But the querystring keys are in the form BatchesForAssignation[0].CenterID,BatchesForAssignation[1].CenterID,BatchesForAssignation[2].CenterID and so on...

Is there's a better way to do this "automagically" so that the model parses the querystring and sends the parsed object to the controller?

Again...Thanks in advance

解决方案

After reviewing this question I've found that the best way to create the model and send it to the controller is creating a CustomModelBinder (from the IModelBinder Interface) and parsing the form's querystring on the BindModel method using the controllerContext.HttpContext.Request.Form property. Something like this:

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

And finally register the new Model Builder in the global.asax file

Sub Application_Start()
    AreaRegistration.RegisterAllAreas()
    ModelBinders.Binders.Add(typeof(ProcessingModel),New ProcessingModelBinder())
    RegisterGlobalFilters(GlobalFilters.Filters)
    RegisterRoutes(RouteTable.Routes)
End Sub

I hope that this helps someone

Regards

这篇关于httppost没有返回值与MVC3剃刀vb.net复杂的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆