asp.net-mvcvb.netajax.beginform

Pass object from view to controller MVC 5


I need to pass this object from the view to the controller:

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...
    }

If I set values in the controller, they are passed to the view; but if I set or change values in the view and pass it back... it gets there, but the values are null.

I'm using this 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"
                                 }
))

my properties are bound as such inside the form:

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

and in my controller:

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

I've also tried <HttpPost> and .HttpMenthod = "POST", but that didn't work either.

Love to know why they're not being set...

The model looks like this:

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

properties that are not in an object, but just types (i.e. Boolean?, String) bind fine, but properties within the object do not.


Solution

  • So, turns out that you can't name your object for what your object is, like:

    Public QuoteVehicle As QuoteVehicle
    

    And then use it like:

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

    All I had to do was rename the property to something that wasn't EXACTLY like the object's type/name... like:

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

    I used Public CurrentQuoteVehicle As QuoteVehicle

    And now this works:

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