【发布时间】: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