【发布时间】:2019-12-09 11:45:28
【问题描述】:
我的 cshtml 中有以下代码用于创建新的数据库条目:
@using (Html.BeginForm())
{
<script type="text/javascript">
function ShowHideAds(button) {
var dAds = document.getElementById("dAds");
if (dAds.style.display == "none") {
dAds.style.display = "block"
}
else {
dAds.style.display = "none"
}
if (lstAds.style.display == "none") {
lstAds.style.display = "block"
}
else {
lstAds.style.display = "none"
document.getElementById('dAds').value = document.getElementById('lstAds').value;
}
}
function ShowHideRg(button) {
var dRg = document.getElementById("dRg");
if (dRg.style.display == "none"){
dRg.style.display = "block"
}
else{
dRg.style.display = "none"
}
if (lstRg.style.display == "none") {
lstRg.style.display = "block"
}
else {
lstRg.style.display = "none"
}
}
</script>
@Html.ValidationSummary(true)
<fieldset>
<legend>New Entry</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ADSName)
</div>
<div class="editor-field">
<div id="lstAds" display="block">
@Html.DropDownListFor(model => model.ADSName, ViewBag.adsName as SelectList, "Choose Name")
<input type="button" value="+" onclick="ShowHideAds(this)" style="text-align: left; color: #F6855E; background: none;
margin: 0; padding: 0; padding-left: 20px; font-weight: bold;
border: none; cursor: pointer;" />
</div>
<div id="dAds" style="display: none;">
@Html.TextBoxFor(model => model.ADSName)
<input type="button" value="x" onclick="ShowHideAds(this)" style="text-align: left; color: #F6855E; background: none;
margin: 0; padding: 0; padding-left: 20px; font-weight: bold;
border: none; cursor: pointer;" />
</div>
@Html.ValidationMessageFor(model => model.ADSName)
</div> }
以及我的 Controller 类中的两个 Create 方法:
public ActionResult Create()
{
List<RightStructure> rightList = entities.RightStructure.ToList();
SelectList listAds = new SelectList(rightList.Select(x => x.ADSName).Distinct(), "ADSName");
ViewBag.adsName = listAds;
return View();
}
[HttpPost]
public ActionResult Create(RightStructure rights)
{
if (ModelState.IsValid)
{
entities.RightStructure .Add(rights);
entities.SaveChanges();
return RedirectToAction("Index");
}
return View(rechte);
}
现在,在单击按钮时,我可以使 DropDownList 消失,从而导致 TextBox 出现,反之亦然。 我想要的是只取当前可见控件的值在我的数据库中创建一个新条目。不幸的是,只有 DropDownList 值总是被我的 RightStructure 对象捕获。所以这意味着 - 如果 DropDownList 是可见的 -> 采用选定的值。如果文本框可见-> 取输入的值。 我试图通过上面的脚本函数来实现这一点,但无法让它工作。我在互联网上搜索时也找不到东西。有人可以帮忙吗?
非常感谢!
【问题讨论】:
-
您正尝试为一个属性绑定一个值 2 次。试试This 答案。它会解决你的问题。
-
我不知道如何将它应用到我的代码中。我已经把它们藏起来了,而且都是Textinput。你能发布一个例子吗?那会很好
-
@Canox 您是否尝试过为表单控件提供唯一的
id:@Html.TextBoxFor(model => model.ADSName,new {@id = "adsnamebox"})和@Html.DropDownListFor(model => model.ADSName, ViewBag.adsName as SelectList, "Choose Name",new {@id="adsdropdown"})。然后尝试在你的 javascript 中获取它们的值,看看你在Controller方法中得到了什么。 -
我应该在哪里传递 ID?但不在函数中?当我问的时候对不起。我用 C# 开发,这是我第一次使用 ASP ord javascript/css。还在学习中。
-
@Canox 您不需要将 id 本身传递给您的函数。您可以使用
Jquery:var adsnameboxvalue= $("#adsnamebox").val();或Javascript:document.getElementById('adsnamebox').value;检索您的值
标签: javascript c# html asp.net-mvc razor