2016-11-12 3 views
-1
public ActionResult MyActionMethod(MyModel model) 
{ 
    //some code 
    string myVar= ActionMethod2(model).toString(); 
    //use myVar 
    Method3(myVar, otherVar); 
} 

public ActionResult ActionMethod2()(MyModel model) 
{ 
    return View(model); 
} 

private Method3(string myVar, int otherVar) 
{ 
    //do something; 
} 

HTMLを返すメソッドを呼び出して、私は.cshtmlビューを返すメソッドを持っているが、ActionMethod2と呼ばれます。
返されたHTMLをアクションメソッドの文字列変数として使用したいと思います。これは可能ですか?上記のサンプルコードとして動作方法

+0

いずれのビューでもActionMethod2()の結果を使用したくないので、変数値として使用したいだけです。 – Elnaz

+1

model-view-controllerタグはパターンに関する質問のためのものです。 ASP.NET-MVCの実装には特定のタグがあります。 –

答えて

0

最初の間違いは、ActionMethod2リターン図であり、あなたはこれを試してみて、それはあなたと一緒に動作しますMyActionMethod

protected string RenderPartialViewToString(string viewName, object model) 
    { 
     if (string.IsNullOrEmpty(viewName)) 
      viewName = ControllerContext.RouteData.GetRequiredString("action"); 

     ViewData.Model = model; 

     using (var sw = new StringWriter()) 
     { 
      ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName); 
      var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw); 
      viewResult.View.Render(viewContext, sw); 

      return sw.GetStringBuilder().ToString(); 
     } 
    } 


public ActionResult MyActionMethod(MyModel model) 
{ 
    //some code 
    //string myVar= ActionMethod2(model).toString(); 
    var myVar = RenderPartialViewToString("yourviewName", model); 
    //use myVar 
    Method3(myVar, otherVar); 
} 

から直接それを得ることができます。

0

これにはContent Methodを使用できます。

public ActionResult ActionMethod2() 
{ 
    return Content("YourHTMLString"); 
} 

それとも、文字列として戻り値の型を設定し、HTML文字列を渡すことができます。

public string ActionMethod2() 
{ 
    return "<html></html>"; 
} 
+0

MyActionMethodからActionMethod2を呼び出すにはどうすればよいですか?文字列はmyVar = ActionMethod2(model).toString()です。正しい? – Elnaz

+0

それは動作しません:string myVar = ActionMethod2(model).toString();空の文字列を返します。 – Elnaz

+0

上記のとおり、ActionMethod2は.cshtmlビューを返します。私は、ActionMethod2()を呼び出し、その文字列として返された値を使用して、その文字列をotherに渡したいと思います。 – Elnaz

0

アプローチ1は、RedirectToAction()メソッドのrouteValuesパラメータの一部として、必要なパラメータを渡すことができます。使用されたクエリ文字列データを使用します。

それとも、のようなクエリ文字列の助けを借りて、それをフレームすることができます

return RedirectToAction("Main", new RouteValueDictionary( 
    new { controller = controllerName, action = "YourActionName", Id = Id})); 

か、TempDataを利用することができます。ブラウザのURLバーで

[HttpPost] 
public ActionResult MyActionMethod(MyModel model) 
{ 
    TempData["myModal"]= new MyModel(); 
    return RedirectToAction("ActionMethod2"); 
} 

[HttpGet] 
public ActionResult ActionMethod2() 
{ 
    MyModel myModal=(MyModel)TempData["myModal"]; 
    return View(); 
} 


このソリューションは一時的なクッキー使用しています:

[HttpPost] 
public ActionResult Settings(SettingsViewModel view) 
{ 
    if (ModelState.IsValid) 
    { 
     //save 
     Response.SetCookie(new HttpCookie("SettingsSaveSuccess", "")); 
     return RedirectToAction("Settings"); 
    } 
    else 
    { 
     return View(view); 
    }  
} 

[HttpGet] 
public ActionResult Settings() 
{ 
    var view = new SettingsViewModel(); 
    //fetch from db and do your mapping 
    bool saveSuccess = false; 
    if (Request.Cookies["SettingsSaveSuccess"] != null) 
    { 
     Response.SetCookie(new HttpCookie("SettingsSaveSuccess", "") { Expires = DateTime.Now.AddDays(-1) }); 
     saveSuccess = true; 
    } 
    view.SaveSuccess = saveSuccess; 
    return View(view); 
} 

かアプローチ4を試してみた: だけでモデルのアクションまたは新しいキーワードにリダイレクトするために必要アクションを呼び出しません。

[HttpPost] 
    public ActionResult MyActionMethod(MyModel myModel1) 
    { 
     return ActionMethod2(myModel1); //this will also work 
    } 
    public ActionResult ActionMethod2(MyModel myModel) 
    { 
     return View(myModel); 
    } 
+0

ようこそスタックオーバーフロー:)もちろん、私は任意のアクションにリダイレクトしたくありません。他のメソッドでは、変数の値としてアクションメソッドの結果を使用したかったのです。 – Elnaz

関連する問題