質問

Ajaxの通話では、文字列値を呼び出しページに返したいと思います。

使用する必要があります ActionResult または、文字列を返しますか?

役に立ちましたか?

解決

あなたはただ使用することができます ContentResult プレーンストリングを返すには:

public ActionResult Temp() {
    return Content("Hi there!");
}

ContentResult デフォルトではaを返します text/plain そのように contentType. 。これは過負荷であるため、次のこともできます。

return Content("<xml>This is poorly formatted xml.</xml>", "text/xml");

他のヒント

また、メソッドが返される唯一のものであることがわかっている場合は、文字列を返すこともできます。例えば:

public string MyActionName() {
  return "Hi there!";
}
public ActionResult GetAjaxValue()
{
   return Content("string value");
}
public JsonResult GetAjaxValue() 
{
  return Json("string value", JsonRequetBehaviour.Allowget); 
}

コントローラーからビューに文字列を返す2つの方法があります

最初

文字列のみを返すことはできますが、htmlファイルに含まれませんjus文字列にはブラウザーに表示されます


2番目

視野の結果として文字列を返すことができます

これを行うためのコードサンプルは次のとおりです

public class HomeController : Controller
{
    // GET: Home
    // this will mreturn just string not html
    public string index()
    {
        return "URL to show";
    }

    public ViewResult AutoProperty()
    {   string s = "this is a string ";
        // name of view , object you will pass
         return View("Result", (object)s);

    }
}

ファイル]を実行します オートプロパティ それはあなたをリダイレクトします 結果 表示して送信します s
表示するコード

<!--this to make this file accept string as model-->
@model string

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Result</title>
</head>
<body>
    <!--this is for represent the string -->
    @Model
</body>
</html>

で実行します http:// localhost:60227/home/autoproperty

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top