2016-06-29 17 views
1

Ich habe zwei Formen, Form1 und Form2, beide sind nur MVC-Formulare. Diese Formen mit zwei verschiedenen Ansichtsmodellen wie unten dargestellt:Zugriff auf Modelldaten in Ajax-Antwort

public class Form1ViewModel 
{ 
    //some public properties 

    public string QueryString { get; set; } 
} 


public class Form2ViewModel 
{ 
    //some public properties 

    public string PreviousQueryString { get; set; } 
} 

In der Steuerung Beitrag Aktion I wie das hier schreiben:

[HttpPost] 
public ActionResult ProcessForm1(Form1ViewModel form1Obj) 
{ 
    //some logic goes here 

    //I'm preparing Querystring from form1 data and appending to Form2 model like 

    Form2ViewModel form2Obj=new Form2ViewModel(); 
    form2Obj.PreviousQueryString = form1Obj.QueryString; 

    return View("Form2",form2Obj) ; 
} 

Und in Form1, ich bin die Einreichung durch JQuery Ajax als

frm.submit(function(ev) { 
    var formData = frm.serialize(); 
    $.ajax({ 
    type: "POST", 
    url: 'ControllerName/ProcessForm1', 
    data: formData, 
    success: function(response) { 
     //Here i need to read the PreviousQueryString and need to push to window.history.pushState() 
    } 
    error: function() {} 
    }); 
}); 

im Ajax Erfolg, muß ich das PreviousQueryString aus der Antwort lesen.

Ich wusste, wie es Client-Seite (mit reinem JS) tun, aber es ist meine Anforderung.

Wie kann ich es tun?

+0

try '@ Request.QueryString [ "PreviousQueryString"]' –

Antwort

1

Versuchen Sie, diese

[HttpPost] 
public string ProcessForm1(Form1ViewModel form1Obj) 
{ 
    JavaScriptSerializer js = new JavaScriptSerializer(); 
    Form2ViewModel form2Obj=new Form2ViewModel(); 
    form2Obj.PreviousQueryString = form1Obj.QueryString; 
     return js.Serialize(form2Obj); 
} 

success: function(response) { 
      var objResponse = $.parseJSON(response); 
     if (objResponse.PreviousQueryString != "") { 
      alert(objResponse.PreviousQueryString); 
     } 
    } 
+0

Response ist in JSON-Daten nicht, es ist eine Gesamtansicht – Prasad

+0

, wo Ihre "Form2" Code anzeigen und Aktion ist? –

+0

Form2 wird eine MVC-Ansicht (.cshtml-Datei) sein und die Post-Aktion selbst wird sie aufrufen – Prasad