首页 > 解决方案 > 是否有任何其他方式访问 HTML:Textboxfor 动作控制器中的内部视图?

问题描述

我从 URL 读取 JSON 并在 MVC 视图 DotnetCore 中显示为表格格式。我在表单控件下的同一视图中有 @Html.TextBoxFor 控件。

为了以表格格式显示,我在视图中同时使用了注册 @model 和 using 语句。所以我无法在控制器操作方法中访问 @Html.TextBoxFor 值。是否有任何访问 HTML:Textboxfor 在操作控制器中的视图内的方式?

@model System.Data.DataTable 
@using System.Data;

无法注册 namespace.models.model

    @using (Html.BeginForm("Index", "Admin", FormMethod.Post))

需要在控制之下访问

{ @Html.TextBoxFor(s => s.ImportJsonUrlName, new { id = "PickUrlID", style = "width:800px" })

点击按钮

<input type="submit" value="Import JSON" />

以表格形式显示

<table class="table table-bordered table-dark">
    <thead>
        <tr>
            @foreach (DataColumn col in Model.Columns)
            {
                <th>@col.ColumnName</th>
            }
        </tr>
    </thead>
    <tbody>
        @foreach (DataRow row in Model.Rows)
        {
            <tr>
                @foreach (DataColumn col in Model.Columns)
                {
                    <td>@row[col.ColumnName]</td>
                }
            </tr>
        }
    </tbody>
</table>

控制器代码:

 [HttpPost]
    public IActionResult Index(AdminModel model)
    {
        DataTable dt = new DataTable();
        string json = (new WebClient()).DownloadString("https://raw.githubusercontent.com/wedeploy-examples/supermarket-web-example/master/products.json");
        dt = JsonConvert.DeserializeObject<DataTable>(json);
        return View(dt);
    }

标签: c#jsonasp.net-core-mvc

解决方案


推荐阅读