【问题标题】:Pass object from view to controller MVC 5将对象从视图传递到控制器 MVC 5
【发布时间】:2017-03-03 18:08:50
【问题描述】:

我需要将此对象从视图传递给控制器​​:

public class QuoteVehicle
    {
        [DisplayName("Quote Vehicle ID")]
        public int QuoteVehicleID { get; set; }
        [DisplayName("Quote ID")]
        public int QuoteID { get; set; }
        [DisplayName("Model Year")]
        public string ModelYear { get; set; }            
        //etc...
    }

如果我在控制器中设置值,它们会被传递给视图;但是如果我在视图中设置或更改值并将其传回......它会到达那里,但值是空的。

我正在使用这个 Ajax.BeginForm:

 @Using (Ajax.BeginForm("GetQuote", "Quote", 
         New With {.QuoteVehicle = JsonConvert.SerializeObject(Model.QuoteVehicle)},
         New AjaxOptions() With {
                                 .UpdateTargetId = "PriceOptionsPanel",
                                 .HttpMethod = "GET",
                                 .OnComplete = "SetDatePickers",
                                 .InsertionMode = InsertionMode.Replace,
                                 .LoadingElementId = "loader"
                                 }
))

我的属性被绑定在表单中:

 @Html.TextBoxFor(Function(model) model.QuoteVehicle.Make, New With {.class = "form-control", .readonly = "readonly"})

在我的控制器中:

 Function GetQuote(QuoteVehicle As String) As ActionResult
    Dim _quoteVehicle = JsonConvert.DeserializeObject(Of QuoteVehicle)(QuoteVehicle)
    Return View(etc.)
 End Function

我也尝试过<HttpPost>.HttpMenthod = "POST",但这也没有用。

很想知道为什么他们没有被设置...

模型如下所示:

Public Class MenuOptionsModel       
        Public Property VIN() As String
        Public Property QuoteVehicle() As QuoteVehicle
        Public Property IsDecoded As Boolean       
        Public Property IsNew As Boolean = False
        Public Property Is30Day As Boolean?
        Public Property IsUnderWarranty As Boolean?
    etc...
End Class

不在对象中的属性,而只是类型(即布尔值?、字符串)可以很好地绑定,但对象中的属性则不能。

【问题讨论】:

  • 为什么要回发刚刚从服务器发送的完全相同的对象?
  • 我为什么不呢?该对象在模型中...我需要用视图中的数据填充该对象 - 我将如何传递该信息? 20 个不同的字段?
  • 我认为您不明白 - 您在服务器上序列化模型,将其发送到客户端,然后发送回完全相同的模型,而不是任何已编辑的模型。删除BeginForm() 的第三个参数并将方法参数更改为您的模型,而不是string
  • 根据您的编辑 - 它必须是 GetQuote(model As MenuOptionsModel)
  • 那你还有其他错误。如果您将 typeof MenuOptionsModel 的模型发送到视图,并使用强类型 HtmlHelper 方法正确生成表单控件,并且 POST 方法中的模型相同,那么它将正确绑定。未绑定的可能原因是,您的模型包含字段而不是属性,参数名称与模型属性之一相同,或者您为复杂对象创建输入。您的模型是否包含名为 model 的属性?

标签: asp.net-mvc vb.net ajax.beginform


【解决方案1】:

将您的操作参数类型更改为 QuoteVehicle 并使用 HttpPost 和 .HttpMenthod = "POST"

 Function GetQuote(model As QuoteVehicle ) As ActionResult
  //now you can access to your model here
    Return View(etc.)
 End Function

【讨论】:

    【解决方案2】:

    所以,事实证明你不能为你的对象命名,比如:

    Public QuoteVehicle As QuoteVehicle
    

    然后像这样使用它:

    Ajax.BeginForm("Action", "Controller", New With { .QuoteVehicle = Model.QuoteVehicle...
    
    Function GetQuote(QuoteVehicle As QuoteVehicle) As ActionResult
       //access properties
    End Function
    

    我所要做的就是将属性重命名为与对象的类型/名称不完全一样的东西...例如:

    Public _quoteVehicle As QuoteVehicle    
    Public NotNamedQuoteVehicle As QuoteVehicle
    etc...
    

    我用Public CurrentQuoteVehicle As QuoteVehicle

    现在可以了:

    Ajax.BeginForm("Action", "Controller", New With { .CurrentQuoteVehicle = Model.QuoteVehicle...
    
    Function GetQuote(CurrentQuoteVehicle As QuoteVehicle) As ActionResult
       //access properties
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-05
      • 2018-11-25
      • 1970-01-01
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多