【问题标题】:Trouble getting textbox value from view to controller in ASP.NET MVC在 ASP.NET MVC 中从视图到控制器获取文本框值时遇到问题
【发布时间】:2020-07-28 00:59:03
【问题描述】:

我还是 ASP.NET MVC 的初学者。我有一个项目,我现在正在使用两个视图和 1 个控制器。 1 个扫描页面、1 个数据输入页面和 1 个名为 LprController 的控制器。

所以在数据输入 cshtml 上,我有一个按钮,我试图从中获取数据,名为 lotnoTBX。

div class="col-md-4 col-sm-offset-3">
    <img src="~/Images/Capture.PNG" class="media-object center-block"/>
    <div class="block-main col-sm-8 col-sm-offset-4">
        <h2>Scan Lot</h2>
        <input id="lotnoTBX"/>
        <div class="container-fluid row">
            <button id="enterBtn" type="submit" class="btn btn-primary btn-group-lg" style="margin-top:5px;" onclick="location.href='@Url.Action("DataEntry", "Lpr")'">Enter</button>
        </div>
    </div>
</div>

然后在控制器页面上事情有点混乱。我不确定在哪里保存或如何保存 lotnoTBX 数据。基本上,我使用网络服务从文本框的结果中获取查询,并使用模型将其发布到数据输入页面。

    public ActionResult ScanPage(FormCollection form)
    { 
        string lotno = form["lotnoTBX"];
        Session["lotno"] = lotno;
        return View();
    } 
    public ActionResult DataEntry()
    {
        FCoai.FCWCFT.PCSInterfaceClient client = new FCWCFT.PCSInterfaceClient("BasicHttpBinding_IPCSInterface1");
        FCoai.FCWCFT.PCSResult x = client.getCurrentLotDetail(Session["lotno"].ToString());
        return View();
    }

然后这是我试图在 cshtml 中发布模型的数据输入页面。

    <table>
        <thead>
            <tr>
                <th class="table-active">OP</th>
                <th class="table-active">Lotno</th>
                <th class="table-active">Itemcode</th>
                <th class="table-active">Marking</th>
                <th class="table-active">Output</th>
            </tr>
            <tr>
                <td @Model.op></td>
                <td @Model.lotno></td>
                <td @Model.itemCode></td>
                <td @Model.marking></td>
                <td @Model.output></td>
            </tr>
        </thead>
    </table>

*edit 我错过了一个可能让某些人感到困惑的操作结果 请帮忙。

【问题讨论】:

  • 您需要创建一个模型类并将其传递给控制器​​方法。控制器方法应该有模型参数。此外,您还需要在 HTML 中使用 name 属性而不仅仅是 idid 属性不会导致发布数据。
  • 谁能帮我解决这个问题,也许有一个例子。抱歉,在提交输入 btn 后,我什至无法返回文本框值。

标签: c# asp.net-mvc


【解决方案1】:

所以我设法让文本框值返回。事实证明,我必须在控制器中使用表单和模型作为参数才能这样做。

这是扫描批次 cshtml

        <h2>Scan Lot</h2>
        @using (Html.BeginForm("ScanPage"))
        {
            @Html.TextBoxFor(Model => Model.lotno)
            <input type="submit" value="Enter"/>
        }

这里是控制器

        public ActionResult ScanPage()
        { 
            return View();
        }  
        [HttpPost]
        public ActionResult ScanPage(FCoai.FCWCFT.PCSResult pCS)
        {
            string lotno = pCS.lotno;
            Session["lotno"] = lotno;
            return RedirectToAction("DataEntry", "Lpr");
        }
        public ActionResult DataEntry()
        {
            FCoai.FCWCFT.PCSInterfaceClient client = new FCWCFT.PCSInterfaceClient("BasicHttpBinding_IPCSInterface1");
            FCoai.FCWCFT.PCSResult x = client.getCurrentLotDetail(TempData["lotno"].ToString());
            //Passing data from controller to view
            ViewData["Lot"] = x;
            return View();
        }
<div class="row">
    <div class="col-md-12">
        <table>
            <thead>
                <tr>
                    <th class="table-active">OP</th>
                    <th class="table-active">Lotno</th>
                    <th class="table-active">Itemcode</th>
                    <th class="table-active">Marking</th>
                    <th class="table-active">Routing</th>
                </tr> 
                <tr>
                    @{ 
                        //This is how display data from view to controller
                        var lot = (FCoai.FCWCFT.PCSResult)ViewData["Lot"];
                    }
                    <td>@lot.op</td>
                    <td>@lot.lotno</td>
                    <td>@lot.itemCode</td>
                    <td>@lot.markLow</td>
                    <td>@lot.rout</td>
                </tr>
            </thead>
        </table>
    </div>
</div>

编辑这是我的最终答案。

尽管我已经设法找到了我自己问题的答案,但如果你们能提供有关约定或其他我可能错过的提示,我将不胜感激。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    相关资源
    最近更新 更多