提交表单时获取查询字符串值... MVC3 [英] Get query string values when submitting a form...MVC3

查看:44
本文介绍了提交表单时获取查询字符串值... MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my MVC3 app, if i type in a query string value in the url and hit enter, I can get the value i typed:

localhost:34556/?db=test

我将触发的默认操作:

public ActionResult Index(string db)

变量db中包含测试".

The variable db has "test" in it.

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

Now, i need to submit a form and read the query string value, but when I submit the form via 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也是如此,我也不知道为什么.提交表单后,有人可以帮我获取查询字符串变量吗?谢谢

The variable dbName is null because Request.QueryString["db"] is null, so is the variable db being passed in and i dont know why. Can someone help me get a query string variable after a form is submitted? Thanks

推荐答案

您可能会遇到

控制器:

[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);
}

ViewModel:

ViewModel:

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

根据您的要求进行了修正.

Fixed it up for your requirement.

这篇关于提交表单时获取查询字符串值... MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆