我有一个网址:
/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/