如何在<;中显示变量;p>;元素在使用razor(C#)和html从POST请求中请求数据之后

如何在<;中显示变量;p>;元素在使用razor(C#)和html从POST请求中请求数据之后,c#,asp.net,visual-studio,razor,server-side,C#,Asp.net,Visual Studio,Razor,Server Side,我注意到,如果我不使用Request[“name”],这将起作用但显然我需要从我的表单中获取信息。我想在确认消息中显示“名称”。我正在使用visual studio asp.net空webapp 这是剃须刀代码 @{ string title = "This is the title"; string name = ""; if (IsPost) { name = Request["name"]; string email =

我注意到,如果我不使用
Request[“name”],这将起作用但显然我需要从我的表单中获取信息。我想在确认消息中显示“名称”。我正在使用visual studio asp.net空webapp

这是剃须刀代码

 @{
    string title = "This is the title";

    string name = "";
    if (IsPost)
    {

        name = Request["name"];
        string email = Request["email"];
        string message = Request["message"];
    }
}


<p>@name</p>
@{
string title=“这是标题”;
字符串名称=”;
如果(IsPost)
{
名称=请求[“名称”];
字符串email=请求[“email”];
字符串消息=请求[“消息”];
}
}
@名字

请告诉我是否应该包含任何其他代码,但我认为这就是您需要查看的全部内容。

控制器:

public ActionResult Test()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Test(string Name, string Message, string email)
    {
        ViewBag.Name = Name;
        ViewBag.Message = Message;
        ViewBag.email = email;
        return View();
    }
Razor视图:

 @using (Html.BeginForm())
{
    <input type="text" name="Name" />
    <input type="text" name="Message" />
    <input type="email" name="email" />
    <input type="submit" value="submit" />
}

<p>@ViewBag.Name</p><br>
<p>@ViewBag.Message</p><br>
<p>@ViewBag.email<p>
@使用(Html.BeginForm())
{
}
@ViewBag.Name


@ViewBag.Message


@ViewBag.email
我想出了一个比其他答案简单得多的解决方案

@{
    string title = "This is the title";

    var names = "";
    var emails = "";

    if (IsPost)
    {
        var name = Request["name"];
        var email = Request["email"];
        string message = Request["message"];

        names = name;
        emails = email;
    }
}
通过重新初始化
请求[“name”]
转换为另一个变量,它允许我在html中使用
名称
变量或
电子邮件
变量。这使得MVC的使用变得冗余且更简单

<p>Name: @names</p>
名称:@names


这不起作用,可能是因为我在asp.net web应用程序环境中工作。很抱歉,我被误解了。请再次检查我编辑的答案。我希望这对你有帮助。谢谢这是我得到的错误“HtmlHelper”不包含“BeginForm”的定义,并且找不到接受类型为“HtmlHelper”的第一个参数的扩展方法“BeginForm”(是否缺少using指令或程序集引用?)您使用的是哪个版本的Mvc。这是在MVC5上100%测试的代码。这很好。也许我应该指定我使用的是visual studio,我的错。我正在visual studio中使用asp.net blank web应用程序