2017-03-03 48 views
0

我需要從視圖到控制器傳遞這個對象:通行證對象到控制器MVC 5

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 

性能不在一個對象,只是類型(即布爾?字符串)綁定很好,但對象內的屬性不。

+0

你爲什麼想回來後正好你剛剛從服務器發送的同一個對象? –

+0

爲什麼我不會?該對象在模型中...我需要用視圖中的數據填充該對象 - 我還會如何傳遞該信息? 20個獨立的領域? –

+1

我不認爲你明白 - 你在服務器上序列化模型,發送給客戶端,併發回完全相同的模型,而不是編輯的任何東西。刪除BeginForm()的第三個參數並改變方法參數爲你的模型,而不是字符串 –

回答

0

所以,事實證明,你不能命名對象爲你的對象是什麼,如:

Public QuoteVehicle As QuoteVehicle 

,然後用它喜歡:

Ajax.BeginForm("Action", "Controller", New With { .QuoteVehicle = Model.QuoteVehicle... 

Function GetQuote(QuoteVehicle As QuoteVehicle) As ActionResult 
    //access properties 
End Function 

所有我需要做的就是重新命名的財產,以SOMET興,這是不完全一樣的對象的類型/名字......這樣的:

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 
0

改變你的動作參數類型,以QuoteVehicle和使用HttpPost和.HttpMenthod =「POST」

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