MVC4查看多个提交按钮 - 更新标签文本 [英] MVC4 View Multiple Submit Buttons - Update label text

查看:213
本文介绍了MVC4查看多个提交按钮 - 更新标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我在MVC4视图中工作多的那一刻提交按钮。为了处理提交不同的按钮,我使用这个类:

<一个href=\"http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx\" rel=\"nofollow\">http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx

我有三个按钮和一个标签:
开始
支持
恢复

我怎么能显示该标签在一定的文字基础上的按钮是pressed?
我婉使用Ajax.BeginForm更新标签文本(所以我没有重新加载网页)。

感谢你在前进!


更新:
例如,当我点击开始按钮的方法将被执行。该方法返回true或false。如何捕捉在标签这个布尔和显示文本的基础上,方法的结果?

更新2:

 &LT; D​​IV&GT;
        &LT;&字段集GT;
            &LT;传奇&GT;联系表格与LT; /传说&GT;
            @ Html.Label(「购股权」)            &LT; D​​IV ID =StartBtn&GT;
                &LT;输入ID =开始TYPE =提交值=开始NAME =行动:开始/&GT;
            &LT; / DIV&GT;
            &LT; D​​IV ID =StandbyBtn&GT;
                &LT;输入ID =待机类型=提交值=待机NAME =操作:待机/&GT;
            &LT; / DIV&GT;            &LT; D​​IV ID =ResumeBtn&GT;
                &LT;输入ID =简历类型=提交值=恢复NAME =行动:恢复/&GT;
            &LT; / DIV&GT;
        &LT; /字段集&GT;
    &LT; / DIV&GT;    [MultipleButton(名称=行动,参数=开始)
    公众的ActionResult开始()
    {
        如果(开始())
        {        }
        其他
        {        }
    }


解决方案

从您的更新,我会用Ajax调用,而不是阿贾克斯形式

  $('。btnSubmit按钮')。在('点击',功能(){
    $阿贾克斯({
        网址:'@ Url.Action('开始','控制')',
        类型:'后',
        数据:{
            按钮:this.id
        }
        数据类型:JSON,
        成功:函数(结果){
            如果(result.Success){
                $('。lblText)文本(result.SetText)。
            }
        }
    });
});

我不知道你想什么传递到你的控制器,但如果你把同一类在所有的按钮(您需要更改它们输入按钮而不是提交也),那么this.id将将是ID点击的按钮,并且将被发送到控制器

那么你的控制器上有一个输入字段匹配的是在数据字段

 公众的ActionResult开始(字符串键){
    //做一点事
    //从这里http://stackoverflow.com/questions/7732481/returning-json-from-controller-never-a-success
    返回JSON(新{成功=真,的setText ='的setText'});
    //其中的setText是你希望你的标签设置为任何。
}

At the moment I am working on a MVC4 view with multiple submit buttons. To handle the submit of the different buttons, I use this class:

http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx

I have three buttons and one label: Start Standby Resume

How can I display a certain text in that label based on which button is pressed? I wan to use Ajax.BeginForm to update the label text (so I do not have to reload the webpage).

Thank you in advance!


Update: For example when I click at the Start Button a method will be executed. This method returns true or false. How to catch this bool and display text in the label, based on the result of the method?

Update 2:

    <div>
        <fieldset>
            <legend>Admin Form</legend>
            @Html.Label("Options")

            <div id="StartBtn">
                <input id="Start" type="submit" value="Start" name="action:Start" />
            </div>
            <div id="StandbyBtn">
                <input id="Standby" type="submit" value="Standby" name="action:Standby" />
            </div>

            <div id="ResumeBtn">
                <input id="Resume" type="submit" value="Resume" name="action:Resume" />
            </div>
        </fieldset>
    </div>   

    [MultipleButton(Name = "action", Argument = "Start")]
    public ActionResult Start()
    {
        if (start())
        {

        }
        else
        {

        }
    }

解决方案

From your update I would use an ajax call instead of the ajax form

$('.btnSubmit').on('click', function(){
    $.ajax({
        url: '@Url.Action('Start', 'Controller')',
        type: 'post',
        data: {
            button: this.id
        }
        dataType: 'json',
        success: function(result){
            if(result.Success){
                $('.lblText').text(result.SetText);
            }
        }
    });
});

I don't know what you want passed to your controller but if you put the same class on all of your buttons (you need to change them to type button instead of submit also) then this.id will will be the id of the clicked button and that will be sent to the controller

then on your controller have an input field matching what is in the data field

public ActionResult Start(string button){
    //do something
    //from here http://stackoverflow.com/questions/7732481/returning-json-from-controller-never-a-success
    return Json(new { Success = "true", SetText = 'SetText' });
    //Where SetText is whatever you want your label set to.
}

这篇关于MVC4查看多个提交按钮 - 更新标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆