【问题标题】:Give value to input box from HttpGet in ASP.NET Core MVC为 ASP.NET Core MVC 中 HttpGet 的输入框赋值
【发布时间】:2020-07-16 03:49:02
【问题描述】:

我正在尝试设置输入框的值,以便可以从其他功能访问它。我想在不使用 ViewBag 的情况下执行此操作,因为我无法从中获取有价值的信息。

我尝试使用 jQuery 访问该值并将其传递给控制器​​,但它返回 null。我也使用过 TempData,但是如果它们直接离开页面,然后再直接返回它就没有用了,因为到那时它将为 0。我想从我的 Search 函数中的 Index 函数访问 id,怎么能我做到这一点?感谢您的宝贵时间!

查看:

<div>
    @model MyApp.Models.AboutModel
    <label>Country:</label>
    <input value="@ViewBag.country" /> //this populates with information from my Country Model
    <input asp-controller="AboutPage" asp-action="Search" type="submit" id="btnSearch" />
</div>

国家:

        [HttpGet("[action]")]
        [Route("/AboutPage/Index/id")]
        public async Task<IActionResult> Index(int id)
        {
            int countryID = id;
            string countryName = "";
            var data = _db.CountryModel.Where(x => x.ID == id).First();
            countryName = data.CountryName;
            ViewBag.country = countryName;
         
            var query = "select * from table where ID = countryID"

            return View(await query);
        }

        [HttpGet]
        public async Task<IActionResult> Search(int id)
        {
            //access the id 
        }

** 添加信息:我的视图是从模型 AboutModel 中提取的,但我从 CountryModel 获取此 ID。我无法在我的视图中调用@Model.id,因为它会返回错误的 ID。

【问题讨论】:

  • 您好,您将如何调用搜索操作?如果是这样,你可以传递 id 在索引中查看,然后使用 'asp-id' 进行搜索。
  • @MichelleWang 感谢您的回复。我不熟悉“asp-id”。对于我来说,asp-route-id 甚至不是智能感知的选择。如果可以的话,我可以将我的 id 变量传递给它。请给我更多信息。谢谢!

标签: razor asp.net-core-mvc http-get viewbag


【解决方案1】:

更新

我想从我的搜索功能中的索引功能访问 id

根据您的评论,我按照您的说法通过@Model.Id传递参数,请参考这段代码:

[HttpGet("[action]")]
[Route("/AboutPage/Index/id")]
public async Task<IActionResult> Index(int id)
{
    int countryID = id;
    string countryName = "";
    var data = _db.CountryModel.Where(x => x.ID == id).First();
    countryName = data.CountryName ;
    ViewBag.country = countryName;

    var query = await  _db.AboutModel.Where(x => x.ID == countryID).FirstAsync();
    return View(query);
}

查看:

    @model MyApp.Models.AboutModel
    @{
        ViewData["Title"] = "Index1";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h1>Index</h1>
<form>
    <div>
        <label>Country:</label>
        <input value="@ViewBag.country" /> //this populates with information from my Country Model
        <input asp-controller="AboutPage" asp-action="Search" asp-route-id="@Model.Id" type="submit" id="btnSearch" />
    </div>
</form>

这是测试结果:

【讨论】:

  • 您好,感谢您的回复。我在我的问题中添加了更多细节。这有点棘手,因为我从与视图中的模型不同的模型中提取 id。此外,我的 Index 中的 return View() 已经在等待一个查询变量,因此添加另一个参数不起作用。返回视图(等待查询)。
  • @BeeSwift,您的索引是返回一个数据还是多个数据?其实我不明白为什么使用@Model.id会得到错误的ID。首先,在你的索引代码中,你把id赋值给countryID,通过countryID过滤表的数据返回视图,那么此时过滤后,数据的ID必须等于countryID,那么它等于 id。
  • 你是对的。那是我的错误。我现在可以拿到身份证了。谢谢。
  • @BeeSwift,好的,我已经根据你的想法更新了我的答案。如果这是有效的,请接受它作为答案。这将帮助有同样问题的人更容易找到答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2020-11-07
  • 2018-04-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多