【问题标题】:MVC read label value and pass to controller on button clickMVC 读取标签值并在按钮单击时传递给控制器
【发布时间】:2016-10-24 14:57:22
【问题描述】:

我有一个 MVC 剃刀视图,上面有部分视图。在主视图中单击按钮后,我必须触发控制器操作。我需要将 label 值传递给此操作。所以我想我可以在填充所有 HTML 之后使用 jQuery 获取标签值。这个label 在部分视图中,我的脚本在主视图中。即使在页面完全加载后,我也会在警报中收到空值。这是我使用的代码:

<input type="button" class="ViewInfo-success" value="View History" onclick="return HistoryResponse()" />
function HistoryResponse() {
    var mac = $('#cmmac').html();
    alert("alert1" + mac);

    try {
        location.replace('/HistoryLookup/GetHistory' + mac);
    }
});

局部视图中的标签代码:

<div class="col-lg-2">
   @Html.Label("MAC Seria:", new { id = "cmmac" })
</div>

<div class="col-lg-2">
   @Html.DisplayFor(x => x.HistoryDO.MACSerial)
</div>

还有其他方法可以实现吗?请输入任何想法。

【问题讨论】:

  • #cmmac 元素是什么?
  • @RoryMcCrossan 我假设它是元素的 id @Html.Label("MAC Seria:", new { id = "cmmac"})
  • 是的,编辑后现在有意义。 @OP 给出的代码你的 URL 将是这样的:'/HistoryLookup/GetHistoryMAC Seria: 这似乎是错误的。您尝试构建的实际 URL 是什么?
  • 我尝试访问的 url 是 /HistoryLookup/GetHistory 我想将该 cmmac 标签的值传递给我的操作,即 HistoryLookup 控制器中的“GetHistory”。该标签值也是使用主视图上引用的模型填充的。

标签: jquery asp.net-mvc asp.net-mvc-4 razor model-view-controller


【解决方案1】:

所以我所要做的就是将模型中的 macid 值存储在这样的隐藏字段中。

@Html.HiddenFor(x => x.Model.historyDO.MACSerial, new { id = "macId", name = "macId" })

主视图上的按钮

<input type="button" class="ViewInfo btn btn-success" value="View History" onclick="return HistoryResponse()" />

historyresponse()方法中通过隐藏字段id获取值

function HistoryResponse() {
        var params = '?MacId=' + $("#macId").val();
        alert("alert3" + params); [![enter image description here][1]][1]
        try {
            location.replace('/HistoryLookup/GetHistory' + params);
        }
        catch (exception) { }
    }

然后发布到行动。

[HttpGet]
public ActionResult GetHistory(string macId)
{    
var model = Repository.GetHistory(macId);
return View("HistoryLookupResponse", model);

}

【讨论】:

    【解决方案2】:

    以下内容对我有用:

    点击前(编辑):

    点击(编辑):

    点击后网址(编辑):

    查看(索引):

    <input type="button" class="ViewInfo-success" value="View History" onclick="HistoryResponse()" />
    
    @Html.Partial("MACSerialPartial")
    
    <script>
        function HistoryResponse() {
            var mac = document.getElementById('cmmac').innerText;
            alert("alert1" + mac);
            location.replace('/HistoryLookup/GetHistory?id=' + mac);
        };
    </script>
    

    带编辑的局部视图 (MACSerialPartial):

    @model mvc_read_label_value.Models.Product
    
    <div class="col-lg-2">
        <p>MAC Serial:</p>
    </div>
    
    <div class="col-lg-2">
        <p id="cmmac">@Model.HistoryDO.MACSerial</p>
    </div>
    

    型号:

    public class Product
    {
        public History HistoryDO { get; set; }
    }
    
    public class History
    {
        public string MACSerial { get; set; }
    }
    

    具有索引操作方法的控制器:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            Product product = new Product() {HistoryDO = new History(){MACSerial = "TestSerial"} };
            return View(product);
        }
    }
    

    HistoryLookup 控制器:

    public class HistoryLookupController : Controller
    {
        public ActionResult GetHistory(string id)
        {
            return View(); // TODO: need to make a view corresponding to this action
        }
    }
    

    【讨论】:

    • 您好 Bwyn,感谢您的详细回复。我需要将 macid 值“testserial”传递给控制器​​。我收到空 cMMac 序列号的警报:但是我需要该标签“testserial”的值我如何捕获它?
    猜你喜欢
    • 1970-01-01
    • 2021-08-16
    • 2021-01-24
    • 2018-01-29
    • 2014-04-08
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    相关资源
    最近更新 更多