モデルとしての文字列 質問する

モデルとしての文字列 質問する

これはもっと簡単な作業であるべきだと思いました。

編集:

今日まで、Asp.Net MVC はこのケースに対して適切な解決策を提供できなかったようです。

単純な文字列をモデルとして渡す必要があり、そのためにさらにクラスなどを定義する必要がない場合は...何か案は ??

単純な文字列をモデルとして渡す


ここでは単純な文字列モデルを作成しようとしています。

次のエラーが発生します:

"Value cannot be null or empty" / "Parameter name: name" 

景色 :

@model string
@using (Html.BeginForm())
{ 
        <span>Please Enter the code</span> 
        @Html.TextBoxFor(m => m) // Error Happens here
        <button id="btnSubmit" title="Submit"></button>
}

コントローラー:

public string CodeText { get; set; }

public HomeController()
{
    CodeText = "Please Enter MHM";
}

[HttpGet]
public ActionResult Index()
{
    return View("Index", null, CodeText);
}

[HttpPost]
public ActionResult Index(string code)
{
    bool result = false;
    if (code == "MHM")
        result = true;

    return View();
}

ベストアンサー1

文字列をモデルとしてビューに渡すよりクリーンな方法があります。ビューを返すときに名前付きパラメータを使用するだけです。

[HttpGet]
public ActionResult Index()
{
    string myStringModel = "I am passing this string as a model in the view";
    return View(model:myStringModel);
}

おすすめ記事