簡體   English   中英

PageData無法在WebMatrix中傳遞數據

[英]PageData fails to pass data in WebMatrix

我正在使用WebMatrix建立一個網站。 我希望用戶在主頁上輸入他們的姓名,並在重定向后將其姓名顯示在另一表格的結果中。 但是我的代碼無法正常工作。

這是主頁的摘要:

@{
    if (IsPost) {
        PageData["fullname"] = String.Format("{0} {1}", Request.Form["mainForename"], Request.Form["mainSurname"]);
        PageData["redir"] = Request.Form["goTo"];
    }
}

<form name="mainForm" id="mainForm" method="post" action="foo.cshtml" onsubmit="return mainValid(this);">
    <h2>Please enter your name:</h2>
    <label for="mainForename" class="label">Forename:</label>
    <input type="text" name="mainForename" id="mainForename">
    <label for="mainSurname" class="label">Surname:</label>
    <input type="text" name="mainSurname" id="mainSurname">
    <input type="submit" name="goTo" value="Go to Form 1">
    <input type="submit" name="goTo" value="Go to Form 2">
</form>

這是主頁指向的頁面的摘要:

@{
    if (IsPost) {
        var display = PageData["fullname"];
    }
}

<form name="form1" id="form1" method="post" onsubmit="return Valid(this);">
    <!-- some HTML code -->
    <input type="submit" name="submit" value="Get results">
    <p>@Html.Raw(display)</p>
</form>

但是無論我在mainForm提交的任何值, PageData["fullname"]PageData["redir"]似乎都沒有值。 問題是什么?

任何幫助,將不勝感激。

我認為PageData僅在將子頁面組合成單個頁面時才有用。

而是嘗試在使用PageData的Session對象。 會話將可用於該用戶的所有頁面。

因此,如果您有PageData [“ fullname”],請使用Session [“ fullname”]

有關更多詳細信息,請參見http://www.mikesdotnetting.com/article/192/transferring-data-between-asp-net-web-pages

我發現您的代碼中的某些功能不是很好:

  1. 為什么將表單操作設置為cshtml文件? 它必須是控制器中的一個動作;
  2. 為什么要使用“硬編碼”表單標簽? 使用@using(@Html.BeginForm('name', 'action', 'controller'...)
  3. 為什么需要WebMatrix? 第一個-它很舊,第二個用於網格-您有一個表單。

使用一個模型,並在@ Html.BeginForm內部使用@Html.TextBoxFor(x=>x.UserName) 然后將表單發布到您要發布的操作中,以重定向到包含第二個表單並具有模型的另一個頁面。 post操作應該看起來像這樣

[HttpPost]
public ActionResult RedirectToAnotherForm(MyModel model)
{
return View('SecondFormView', new SecondFormModel{
userName = model.name
})
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM