2014-03-19 38 views
37

我想知道,有什麼技術,所以我們可以在RedirectToAction我們可以在RedirectToAction中將模型作爲參數傳遞嗎?

對於示例轉發Model作爲參數:

public class Student{ 
    public int Id{get;set;} 
    public string Name{get;set;} 
} 

控制器

public class StudentController : Controller 
{ 
    public ActionResult FillStudent() 
    { 
     return View(); 
    } 
    [HttpPost] 
    public ActionResult FillStudent(Student student1) 
    { 
     return RedirectToAction("GetStudent","Student",new{student=student1}); 
    } 
    public ActionResult GetStudent(Student student) 
    { 
     return View(); 
    } 
} 

我的問題 - 我可以通過RedirectToAction中的學生模型?

+1

當然,這是一個有效的通話。它不會被路由嗎? –

+0

我試過了,但沒有找到模型值 – Amit

+0

由於路徑字典處理對象,嘗試改變'GetStudent'動作來接受'對象'並將其轉換爲'Student'。另一個選擇是在從'FillStudent'傳遞它時使用JSON序列化它。 –

回答

48

使用TempData

表示一組數據的持續只從一個請求到 下

[HttpPost] 
public ActionResult FillStudent(Student student1) 
{ 
    TempData["student"]= new Student(); 
    return RedirectToAction("GetStudent","Student"); 
} 

[HttpGet] 
public ActionResult GetStudent(Student passedStd) 
{ 
    Student std=(Student)TempData["student"]; 
    return View(); 
} 

替代方式 傳遞使用查詢數據字符串

return RedirectToAction("GetStudent","Student", new {Name="John", Class="clsz"}); 

,這將產生一個GET請求一樣Student/GetStudent?Name=John & Class=clsz

確保您想重定向到與[HttpGet]爲 上述RedirectToAction裝飾會發出GET,HTTP狀態 碼302找到請求的方法(共同執行url重定向的方式)

+5

我不想使用'TempData'。我知道我們可以使用TempData來實現。但我的問題是不同的 – Amit

+0

@AmitAgrawal,檢查第二種方法 –

+1

我也知道方法2,但我的問題是不同的我可以實現我的問題__是/否_ – Amit

19

只需撥打redirect to actionnew模型關鍵字的動作即可。

[HttpPost] 
    public ActionResult FillStudent(Student student1) 
    { 
     return GetStudent(student1); //this will also work 
    } 
    public ActionResult GetStudent(Student student) 
    { 
     return View(student); 
    } 
+0

什麼是__返回GetStudent(student1)__ – Amit

+0

@AmitAgrawal它會調用你的GetStudent()動作 – Rex

+6

動作GetStudent必須返回與模型學生一起查看'GetStudent' - 爲我工作 - **返回查看(「GetStudent」,學生)** – hotfusion

-2

我找到了這樣的東西,有助於擺脫硬編碼的tempdata標籤

public class AccountController : Controller 
{ 
    [HttpGet] 
    public ActionResult Index(IndexPresentationModel model) 
    { 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult Save(SaveUpdateModel model) 
    { 
     // save the information 

     var presentationModel = new IndexPresentationModel(); 

     presentationModel.Message = model.Message; 

     return this.RedirectToAction(c => c.Index(presentationModel)); 
    } 
} 
+1

這個方法來自哪裏? 「this.RedirectToAction」 –

+0

好吧,控制器有這種方法:) – DiSaSteR

+1

我無法找到RedirectToAction的重載,它需要一個lambda表達式。你確定這是在System.Web.Mvc中,而不是在你使用的第三方庫或擴展方法中? –

7

是的,你可以通過你已經使用

return RedirectToAction("GetStudent", "Student", student1); 

假設student1所示模型是Student

的情況下,這將產生以下網址(使用默認路由和你的假設student1的值爲ID=4Name="Amit"

.../Student/GetStudent/4?Name=Amit

在內部,RedirectToAction()方法通過使用模型中每個屬性的值.ToString()來構建RouteValueDictionary。但是,只有在模型中的所有屬性都是簡單屬性時,綁定纔會起作用,如果任何屬性都是複雜對象或集合,則綁定將失敗,因爲該方法不使用遞歸。如果例如,Student包含屬性List<string> Subjects,那麼將導致

....&Subjects=System.Collections.Generic.List'1[System.String]

的查詢字符串值屬性和綁定會失敗,該屬性將null

0
[HttpPost] 
    public async Task<ActionResult> Capture(string imageData) 
    {      
     if (imageData.Length > 0) 
     { 
      var imageBytes = Convert.FromBase64String(imageData); 
      using (var stream = new MemoryStream(imageBytes)) 
      { 
       var result = (JsonResult)await IdentifyFace(stream); 
       var serializer = new JavaScriptSerializer(); 
       var faceRecon = serializer.Deserialize<FaceIdentity>(serializer.Serialize(result.Data)); 

       if (faceRecon.Success) return RedirectToAction("Index", "Auth", new { param = serializer.Serialize(result.Data) }); 

      } 
     } 

     return Json(new { success = false, responseText = "Der opstod en fejl - Intet billede, manglede data." }, JsonRequestBehavior.AllowGet); 

    } 


// GET: Auth 
    [HttpGet] 
    public ActionResult Index(string param) 
    { 
     var serializer = new JavaScriptSerializer(); 
     var faceRecon = serializer.Deserialize<FaceIdentity>(param); 


     return View(faceRecon); 
    } 
+0

您能否請您在視圖中展示您如何使用faceRecon? – justitan

相關問題