【问题标题】:ASP.Net MVC choose DropDownList/TextBox for editing depending on which one is visibleASP.Net MVC 选择 DropDownList/TextBox 进行编辑,具体取决于哪个可见
【发布时间】: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 =&gt; model.ADSName,new {@id = "adsnamebox"})@Html.DropDownListFor(model =&gt; 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


【解决方案1】:

首先,感谢 Rahul Sharma 和 JayNaz。

我找到了解决方案,但我还需要一步。

我用 DropDownList 和 TextBox 代替了 DropDownListFor 和 TextBoxFor:

@Html.DropDownList("ADSlist", ViewBag.adsName as SelectList, "Choose Name")
@Html.TextBox("ADSTxt")

所以现在在我的控制器中我可以这样做:

var value = Request["ADSList"];
var value2 = Request["ADSTxt"];

现在我只需要知道哪个控件当前是可见的。有人知道如何实现吗?

谢谢!

编辑:

知道了。刚刚在我的函数中添加了以下内容:

        var txtBox = "Hidden";
        $.post("/Rights/GetState", { txtAds: txtBox });

...当文本框可见时为“可见”。所以现在我还在我的 Controller 中添加了一个新方法:

    [HttpPost]
    public ActionResult GetState(string txtAds, string txtRg)
    {
        stateTxtAds = txtAds;
        stateTxtRg = txtRg;
        return View();
    }

所以我可以设置全局变量 stateTxtAds 和 stateTxtRg 并在我的 Create Method 中使用它们。

【讨论】:

    猜你喜欢
    • 2016-07-07
    • 1970-01-01
    • 2023-03-23
    • 2017-03-03
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    相关资源
    最近更新 更多