【问题标题】:Ready ViewBag values from controller?准备好来自控制器的 ViewBag 值了吗?
【发布时间】:2017-01-04 13:49:58
【问题描述】:

在我的控制器中,我有以下 ViewBag,其中包含下拉列表的硬编码值

public ActionResult Order(string id, string printRequest)
{                            
this.ViewBag.PartialReasons = new List<string>() {" ", "Insufficient stock", "Suspended", "Retired", "Ordered Incorrectly", "Unable to deliver" };
}

我想要做的是,一旦从下拉列表中选择“无法交付”值,然后弹出一个带有“HELLO EVERYONE”的警报,然后用户应该能够点击并保存,我现有的保存功能onclick如下,

简而言之,只是想从控制器 viewbag 中读取一个值。

    $('.pcss-save').click(function() {
        if (CheckSerialNumbersForQuantity()) {
            if (CheckReasons()) { //If Ordered incrrectly
                $('#genericmodal').find(".pcss-submit-genericmodal").unbind("click");
                $('#genericmodal').find(".modal-title").html("Confirmation");
                $('#genericmodal').find(".modal-body").html("This order will be Cancelled");
                $('#genericmodal').modal('show');
                $('#genericmodal').find(".pcss-submit-genericmodal").click(function() {
                    $("#orderform").submit();
                });
            } else {
                $("#orderform").submit();
            }
        }
    });

【问题讨论】:

    标签: javascript c# jquery model-view-controller


    【解决方案1】:

    先问几个问题:

    • 如何将 Viewbag 值加载到下拉列表中?
    • 您使用的是 Razor 视图吗?
    • 您似乎要问多个问题,所以我将尝试逐个回答。

    通常下拉菜单是键值对。使用 Razor 视图,您可以使用:

    @Html.DropDownList("reasonListID", new SelectList(ViewBag.PartialReasons, "reasonCode", "reasonName"))
    

    这假定 viewbag 对象是具有两个属性 reasonCodereasonName 的对象列表。

    纯字符串列表并不能真正用于驱动逻辑,您希望将潜在值编入代码,以便让自己进行清晰和轻松的比较。通过这种方式,您可以简单地查看reasonCode 而不必编写代码来将您的选择值与“一些可能在代码中没有太多意义的长字符串”进行比较,例如这可能在您的 CheckReasons() 函数中。

    if CheckReasons() == "UD") {
       showModal( "HELLO EVERYONE");
    }
    

    容易
    if CheckReasons() == "Unable to deliver") {
       showModal("HELLO EVERYONE");
    }
    

    就弹出模式而言,看起来您正在使用 jquery,所以看看这个回答的问题:MVC3 Razor and Modal popup

    【讨论】:

    • 在控制器中我有一个带有硬编码值的 viewbag,我想在警报中传递该 viewbag 值,即在 javascript 中分配 viewbag 值,然后说它是否等于某个值然后发出警报
    【解决方案2】:

    在您看来,您应该可以使用 @ViewBag.PartialReasons 直接调用您的 ViewBag,并且当 Razor 引擎呈现脚本时,它将使用 @ViewBag.PartialReasons 中的这些值。

    你可能想做这样的事情。

        $('#yourDropDownID').on('change', function(){
            if(CheckReasons()){
    
              var genericModal = $('#genericmodal'); //Avoids rescanning the dom.
                genericModal.find(".pcss-submit-genericmodal").unbind("click");
                genericModal.find(".modal-title").html("Confirmation");
                genericModal.find(".modal-body").html("This order will be Cancelled");
                genericModal.modal('show');
                genericModal.find(".pcss-submit-genericmodal").click(function() {
                    $("#orderform").submit();
            }
        });
    

    您的支票原因可能是这样的。这是一种开放的解释。

    function CheckReasons(){
        var myReason = @String.Join(ViewBag.PartialReasons.ToArray());
    
        //loop and figure it out if your reason is selected and return true or false
    }
    

    这一切都是在我的脑海中写下的,因此可能需要进行一些调整,但这应该可以帮助您开始您想要做的事情。

    更新 您无法在浏览器中访问 ViewBag,因为它只存在于服务器端。您需要使用 ViewBag 在服务器上渲染完成您想要的脚本。 Razor 引擎将填充 C# 代码的输出。

    【讨论】:

    • 你不能通过客户端访问它,你可以通过剃刀引擎渲染出值。 function CheckReasons(){ var myReason = @String.Join(ViewBag.PartialReasons.ToArray()); //loop and figure it out if your reason is selected and return true or false }
    • 我该怎么做@destructi6n
    • this post。这可能会让你走上正确的理解之路
    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 2015-11-08
    • 2017-12-23
    • 2016-07-17
    • 2017-01-07
    相关资源
    最近更新 更多