简体   繁体   English

如何将信息作为参数从视图传递到控制器

[英]How do I pass information from my View to my Controller as a Parameter

I have a form and two SQL databases. 我有一个表单和两个SQL数据库。 The Form is to submit information to Database 1, but when the user enters information into the first field I need it to scan Database 2 for this data and populate a Description from this. 表单是向数据库1提交信息,但是当用户在第一个字段中输入信息时,我需要它扫描数据库2中的该数据并从中填充说明。

I currently have a button that will run script in the controller to retrieve this data. 我目前有一个按钮,它将在控制器中运行脚本以检索此数据。 However, I don't know how to pass the first field as a parameter to the button onclick so it will use this to search the database. 但是,我不知道如何将第一个字段作为参数传递给按钮onclick,因此它将使用它来搜索数据库。

Everything works in the controller but I don't know how to take information that is typed in an EditorFor text field and submit this to the button as a parameter. 一切都在控制器中正常工作,但我不知道如何获取在EditorFor文本字段中键入的信息,并将其作为参数提交给按钮。 I have tried using the 'Model' to do this but my understanding of how this works is limited. 我曾尝试使用“模型”来执行此操作,但是我对它的工作原理了解有限。

My View: 我的观点:

<div class="form-group">
        @Html.LabelFor(model => model.Number, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-4">
            @Html.EditorFor(model => model.Number, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Number, "", new { @class = "text-danger" })
        </div>
        <div class="col-md-1">
            <input type="button" value="Search" class="btn btn-default" onclick="location.href='@Url.Action("Search", "Catalog", new { Number = Model.Number })'" ; />
        </div>
    </div>

My Controller: 我的控制器:

 public ActionResult Search(string catNo = "10004361")
        {
            IFS_Catalog_Products DatabaseScanner = new IFS_Catalog_Products();
            string catDesc = "";

            if (ModelState.IsValid)
            {
                SqlConnection con = new SqlConnection();
                con.ConnectionString = @"CONNECTION STRING";
                con.Open();

                SqlCommand cmd = new SqlCommand("SELECT DESCRIPTION FROM TABLE WHERE NUMBER = '" + catNo + "'", con);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    catDesc = dr.GetString(0);
                    //break;
                }
                    con.Close();
            }
            DatabaseScanner.IFS_Catalog_Desc = catDesc;
            DatabaseScanner.IFS_Catalog_No = catNo;

            return View("Create", DatabaseScanner);
        }

The code compiles correctly and all aspects of the code work except for the Model.Number only ever passes a Null into the script. 代码可以正确编译,并且除了Model.Number之外,代码的所有其他方面都可以正常工作,只有将Null传递给脚本。

I want to take the information from model.Number within the EditorFor field and use this within the statement. 我想从EditorFor字段中的model.Number中获取信息,并在语句中使用它。

您需要使用@Html.ActionLink帮助器而不是button来实现目标,如下所示:

@Html.ActionLink("Search", "Search", "Catalog", new { catNo = Model.Number }, new { @class="btn btn-default" })

I would use the POST action like this: 我将使用POST操作,如下所示:

  1. your view: 您的看法:

     @model YOUR_MODEL @using (Html.BeginForm("Search", "Home", FormMethod.Post)) { <div class="form-group"> @Html.LabelFor(model => model.Number, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-4"> @Html.EditorFor(model => model.Number, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Number, "", new { @class = "text-danger" }) </div> <div class="col-md-1"> <input type="button" value="Search" class="btn btn-default"/> </div> 

    } }

  2. your model: 您的模型:

     public class YOUR_MODEL { public string Number {get;set;} } 
  3. your controller 您的控制器

     [HttpPost] public ActionResult Search(YOUR_MODEL model) { IFS_Catalog_Products DatabaseScanner = new IFS_Catalog_Products(); string catDesc = ""; if (ModelState.IsValid) { SqlConnection con = new SqlConnection(); con.ConnectionString = @"CONNECTION STRING"; con.Open(); SqlCommand cmd = new SqlCommand("SELECT DESCRIPTION FROM TABLE WHERE NUMBER = '" + model.Number+ "'", con); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { catDesc = dr.GetString(0); //break; } con.Close(); } DatabaseScanner.IFS_Catalog_Desc = catDesc; DatabaseScanner.IFS_Catalog_No = catNo; return View("Create", DatabaseScanner); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM