Asp.net mvc 将对象从视图传递到控制器MVC 5
我需要将此对象从视图传递到控制器:Asp.net mvc 将对象从视图传递到控制器MVC 5,asp.net-mvc,vb.net,ajax.beginform,Asp.net Mvc,Vb.net,Ajax.beginform,我需要将此对象从视图传递到控制器: public class QuoteVehicle { [DisplayName("Quote Vehicle ID")] public int QuoteVehicleID { get; set; } [DisplayName("Quote ID")] public int QuoteID { get; set; } [DisplayName("Model Year")]
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...
}
Function GetQuote(QuoteVehicle As String) As ActionResult
Dim _quoteVehicle = JsonConvert.DeserializeObject(Of QuoteVehicle)(QuoteVehicle)
Return View(etc.)
End Function
如果我在控制器中设置值,它们将被传递到视图;但是如果我在视图中设置或更改值并将其传回。。。它到达那里,但值为空
我正在使用这个Ajax.begin:
@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"})
在我的控制器中:
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...
}
Function GetQuote(QuoteVehicle As String) As ActionResult
Dim _quoteVehicle = JsonConvert.DeserializeObject(Of QuoteVehicle)(QuoteVehicle)
Return View(etc.)
End Function
我也尝试过
和.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
不在对象中但只有类型(即布尔?、字符串)的属性绑定良好,但对象中的属性绑定不好。将操作参数类型更改为QuoteHicle,并使用HttpPost和.HttpMenthod=“POST”
所以,事实证明,你不能根据你的对象来命名你的对象,比如:
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 currentQuoteHicle作为QuoteHicle
现在,这是可行的:
Ajax.BeginForm("Action", "Controller", New With { .CurrentQuoteVehicle = Model.QuoteVehicle...
Function GetQuote(CurrentQuoteVehicle As QuoteVehicle) As ActionResult
//access properties
End Function
为什么你想发回与你刚从服务器发送的完全相同的对象?为什么我不想?对象在模型中。。。我需要用视图中的数据填充该对象-否则我将如何传递该信息?20个单独的字段?我想你不明白-你在服务器上序列化模型,发送到客户端,然后发送回完全相同的模型,而不是任何经过编辑的内容。删除
BeginForm()
的第三个参数,并将方法参数更改为您的模型,而不是字符串
,并根据您的编辑-它必须是GetQuote(模型为MenuOptions model)
,然后您会出现其他错误。如果将类型为MenuOptionsModel
的模型发送到视图,并使用强类型的HtmlHelper
方法正确生成表单控件,并且POST方法中的模型相同,则将正确绑定该模型。不绑定的可能原因有:您的模型包含字段而不是属性,参数名称与模型属性之一相同,或者您为复杂对象创建了输入。您的模型是否包含名为model
的属性?