如何根据 cshtml 中的 ID 获取项目的价值?
How can get value of an item based on ID in cshtml?
假设我有一个带有特定 ID 和倍数选项的 select 标签。然后在下面,我想显示基于 selected 的部分视图。我想使用 if else 语句,但我不知道如何使用 c# 获得 select 的值。
我的想法是这样的
<select id="selectItem">
<option value="A">A</option>
<option value="B">A</option>
<option value="C">A</option>
</select>
@if ('selectItem' == "A"){
<partial name="..."/>
}
你可以用js做,这里有一个demo:
控制器(测试):
public IActionResult Index()
{
return View();
}
public IActionResult Partial()
{
return PartialView("Partial1");
}
部分 1:
<h1>Partial1</h1>
Index.cshtml:
<select id="selectItem">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
<div id="PartialContent"></div>
js:
<script>
$(function () {
getPartial();
});
$("#selectItem").change(function () {
getPartial();
});
function getPartial() {
if ($("#selectItem").val() == "C") {
$.ajax({
type: 'GET',
url: "/Test/Partial",
dataType: 'html', //dataType - html
success: function (result) {
//Create a Div around the Partial View and fill the result
$('#PartialContent').html(result);
}
});
}else {
$('#PartialContent').html("");
}
}
</script>
结果:
假设我有一个带有特定 ID 和倍数选项的 select 标签。然后在下面,我想显示基于 selected 的部分视图。我想使用 if else 语句,但我不知道如何使用 c# 获得 select 的值。
我的想法是这样的
<select id="selectItem">
<option value="A">A</option>
<option value="B">A</option>
<option value="C">A</option>
</select>
@if ('selectItem' == "A"){
<partial name="..."/>
}
你可以用js做,这里有一个demo: 控制器(测试):
public IActionResult Index()
{
return View();
}
public IActionResult Partial()
{
return PartialView("Partial1");
}
部分 1:
<h1>Partial1</h1>
Index.cshtml:
<select id="selectItem">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
<div id="PartialContent"></div>
js:
<script>
$(function () {
getPartial();
});
$("#selectItem").change(function () {
getPartial();
});
function getPartial() {
if ($("#selectItem").val() == "C") {
$.ajax({
type: 'GET',
url: "/Test/Partial",
dataType: 'html', //dataType - html
success: function (result) {
//Create a Div around the Partial View and fill the result
$('#PartialContent').html(result);
}
});
}else {
$('#PartialContent').html("");
}
}
</script>
结果: