c# - 如何在表单帖子中获取查询字符串值

标签 c# asp.net-mvc

我有一个网址:

/Account.aspx/Confirm/34a1418b-4ff3-4237-9c0b-9d0235909d76

和一个表格:

<% using (Html.BeginForm())
   { %>
    <fieldset>
        <p>
            <label for="password" class="instructions">
                Contraseña:</label>
            <%= Html.Password("password") %>
            <%= Html.ValidationMessage("password", "*") %>
        </p>
        <p>
            <input type="submit" value="Validar" />
        </p>
    </fieldset>
<% } %>

在 Controller 的 Action 中:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Confirm(string id, string password)
{
    //code
}

我想获取URL中GUID的值(Confirm后的部分)和输入的密码的值。

我该怎么做?

编辑:

我已经注册了这条路线:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.IgnoreRoute("Error.aspx/{*pathInfo}");
    routes.IgnoreRoute("Admin/{*pathInfo}");
    routes.MapRoute(
        "Default",
        "{controller}.aspx/{action}/{id}",
        new { controller = "Home", action = "Index", id = "" } 
    );
    routes.MapRoute("Root", "", 
            new { controller = "Home", action = "Index", id = "" });
}

我觉得有什么地方很奇怪,但在 Confirm 操作的 id 参数中我得到一个空字符串。

最佳答案

这不是因为您发布的表单在表单中没有 ID 吗?

您可以做的是在您的表单页面上将模型设置为传入的 ID,然后为该 ID 分配一个隐藏的输入值...

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage<string>" %>
public ActionResult Confirm(string id)
{
   return View(id);
}

现在 ViewData.Model 将包含您的 ID。将其放入您的表单中。

<input type="hidden" id="id" name="id" value="<%= ViewData.Model %>" />

然后当您提交表单时,它会传递 ID。

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Confirm(string id, string password)
{
   //...now you have access to ID and password
}

关于c# - 如何在表单帖子中获取查询字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/748658/

相关文章:

c# - ASP.NET MVC - 如何首先使用实体​​框架模型/数据库部署到 Azure?

c# - Uri.IsWellFormedUriString 用于相对 Hashbang url 兼容性

c# - Image.FromFile() 或 FromStream() 在 GdiplusStartup 上挂起

c# - 从动态对象 C# 中读取值

c# - 无法将 Int 类型转换为 List

javascript - 如何为模板重用 Kendo UI 组件?属性 ".name"在 DOM 中为多个 HTML 元素提供完全相同的 ID

c# - 将格式应用于 DataTable 中的列

C# void 类型-安全

c# - ASP.NET MVC 中的 Ajax 文件上传和 JQuery 表单数据

asp.net-mvc - F# 中的异步 Controller 操作