提交表单时获取查询字符串值...MVC3

2024-05-14

在我的 MVC3 应用程序中,如果我在 url 中输入查询字符串值并按 Enter 键,我可以获得输入的值:

localhost:34556/?db=test

我将触发的默认操作:

public ActionResult Index(string db)

变量 db 中有“test”。

现在,我需要提交表单并读取查询字符串值,但是当我通过 jQuery 提交表单时:

       $('#btnLogOn').click(function(e) {
         e.preventDefault();
             document.forms[0].submit();
         });

以下是我发送的表格:

   @using (Html.BeginForm("LogIn", "Home", new { id="form1" }, FormMethod.Post))

动作如下:

  [HttpPost]
    public ActionResult LogIn(LogOnModel logOnModel, string db)
    {
        string dbName= Request.QueryString["db"];
     }

变量 dbName 为 null,因为 Request.QueryString["db"] 为 null,传入的变量 db 也是 null,我不知道为什么。有人可以帮我在提交表单后获取查询字符串变量吗?谢谢


你可以有类似的东西

控制器:

[HttpGet]
public ActionResult LogIn(string dbName)
{
    LogOnViewModel lovm = new LogOnViewModel();
    //Initalize viewmodel here
    Return view(lovm);

}

[HttpPost]
public ActionResult LogIn(LogOnViewModel lovm, string dbName)
{
    if (ModelState.IsValid) {
        //You can reference the dbName here simply by typing dbName (i.e) string test = dbName;
        //Do whatever you want here. Perhaps a redirect?
    }
    return View(lovm);
}

视图模型:

public class LogOnViewModel
{
    //Whatever properties you have.
}

编辑:根据您的要求修复它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

提交表单时获取查询字符串值...MVC3 的相关文章

随机推荐