【发布时间】: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