简体   繁体   English

PageData无法在WebMatrix中传递数据

[英]PageData fails to pass data in WebMatrix

I am building a website with WebMatrix. 我正在使用WebMatrix建立一个网站。 I would like users to enter their name in the main page and after redirection their name will be shown in the results of another form. 我希望用户在主页上输入他们的姓名,并在重定向后将其姓名显示在另一表格的结果中。 But my code is not working. 但是我的代码无法正常工作。

This is a snippet of the main page: 这是主页的摘要:

@{
    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>

This is a snippet of the page that the main page directs to: 这是主页指向的页面的摘要:

@{
    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>

But whatever value I have submitted in the mainForm , PageData["fullname"] and PageData["redir"] seem to have no values. 但是无论我在mainForm提交的任何值, PageData["fullname"]PageData["redir"]似乎都没有值。 What is the problem? 问题是什么?

Any help would be appreciated. 任何帮助,将不胜感激。

I think PageData is only useful when combining subpages into a single page. 我认为PageData仅在将子页面组合成单个页面时才有用。

Instead, try the Session object where you are using PageData. 而是尝试在使用PageData的Session对象。 Session will be available for all that user's pages. 会话将可用于该用户的所有页面。

So where you have PageData["fullname"] use Session["fullname"] 因此,如果您有PageData [“ fullname”],请使用Session [“ fullname”]

For more details, see http://www.mikesdotnetting.com/article/192/transferring-data-between-asp-net-web-pages 有关更多详细信息,请参见http://www.mikesdotnetting.com/article/192/transferring-data-between-asp-net-web-pages

I find something that's not quite good in your code: 我发现您的代码中的某些功能不是很好:

  1. Why your form action is set to a cshtml file? 为什么将表单操作设置为cshtml文件? It has to be an action in a controler; 它必须是控制器中的一个动作;
  2. Why are you using "hardcoded" form tag? 为什么要使用“硬编码”表单标签? Use @using(@Html.BeginForm('name', 'action', 'controller'...) 使用@using(@Html.BeginForm('name', 'action', 'controller'...)
  3. Why do you need WebMatrix? 为什么需要WebMatrix? 1st - its pretty old, 2nd it for grids - you have a form. 第一个-它很旧,第二个用于网格-您有一个表单。

Use a model, and use @Html.TextBoxFor(x=>x.UserName) inside the @Html.BeginForm. 使用一个模型,并在@ Html.BeginForm内部使用@Html.TextBoxFor(x=>x.UserName) Then post the form in the action you are posting to redirect to another page that contains the 2nd Form, and have a model. 然后将表单发布到您要发布的操作中,以重定向到包含第二个表单并具有模型的另一个页面。 The post action should look somehow like this 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