从URL获取参数并将其放入输入控件的简单方法
本文关键字:输入 控件 方法 简单 获取 URL 参数 | 更新日期: 2023-09-27 18:14:59
我正在制作一个邀请系统,其中用户在注册到网站时可以指定一个用户谁推荐他们。
现有用户也可以发送邀请。朋友将收到一个链接:
http://www.foo.com/account/register?referal=sandyUser216
我如何获得该值sandyUser216
并将其作为文本输入框内的值?
我使用c#和mvc。
检查Request.QueryString
<input type="text" value="@Request.QueryString["referal"]" />
在ASP中总是。. NET MVC应用程序,首先编写一个视图模型,它将表示视图中包含的信息:
public class RegisterViewModel
{
[Required]
public string Referal { get; set; }
}
然后编写控制器动作分别显示注册表单和处理它:
public ActionResult Register(RegisterViewModel model)
{
return View(model);
}
[HttpPost]
[ActionName("Register")]
public ActionResult ProcessRegistration(RegisterViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
// TODO: perform the registration
return RedirectToAction("success");
}
,最后编写相应的强类型视图:
@model RegisterViewModel
@using (Html.BeginForm())
{
@Html.LabelFor(x => x.Referal)
@Html.EditorFor(x => x.Referal)
@Html.ValidationMessageFor(x => x.Referal)
<button type="submit">Register</button>
}
现在剩下的就是导航到/account/register?referal=sandyUser216
。
你已经完成了整个MVC模式。如果你跳过这三个字母中的任何一个,就意味着你在做ASP。. NET MVC错误