,同样的形式更新的标签MVC 5按钮单击事件 [英] MVC 5 button click event that updates a label on the same form

查看:112
本文介绍了,同样的形式更新的标签MVC 5按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MVC 5,是否有可能有在同一页面上更新标签的按钮单击事件?

Using MVC 5, is it possible to have a button click event that updates a label on the same page?

例如,如果我有一个结构

For example, if I have a structure

@using (Html.BeginForm()) {
    <fieldset>
        <legend>Form</legend>
        <p>
            @Html.TextBox("textbox1")
        </p>
        <p>
            @Html.Label("label1")
        </p>
        <p>
            <input type="submit" value="Submit" />
        </p>
    </fieldset>
}

点击提交它根据被调用时,功能键抓住 TextBox1中的价值,还可以修改在点击提交按钮,然后更新标签作为结果的价值?

Clicking the Submit button grabs the textbox1's value, modifies it according to the function that gets called when the submit button is clicked and then update the value of the label as the result?

假设我的控制器被称为 TestController.cs ,这是首页

Assuming my controller is called TestController.cs and this is all done on the Index page

我注意到一些<一个href=\"https://stackoverflow.com/questions/19007827/process-result-and-display-in-same-page-asp-net-mvc\">suggestions包括使用AJAX(新的我)

I noticed some suggestions include using AJAX (new to me)

推荐答案

您不要的不一定的需要使用AJAX这一点。所有你需要做的是通过你的标签值回落为你的行动结果例如一部分。

You don't necessarily need to use AJAX for this. All you need to do is pass the value of your label back down as part of your action result e.g.

控制器

public class TestController : Controller
{
    public ActionResult Index(string label)
    {
        // pass label value into view
        return View("Index", label ?? "");
    }

    [HttpPost]
    public ActionResult Index(string textValue)
    {
        // do something with textValue
        // redirect to our Index action passing the new label value
        return RedirectToAction("Index", textValue);
    }
}

查看

@model string

@using (Html.BeginForm()) {
    <fieldset>
    <legend>Form</legend>
    <p>
        @Html.TextBox("textbox1")
    </p>
    <p>
        @Html.Label("label", Model)
    </p>
    <p>
        <input type="submit" value="Submit" />
    </p>
    </fieldset>
}

这种方法的好处是它符合邮政/重定向/获取模式,所以如果你刷新页面不会再次尝试重新提交表单。

The benefit of this approach is it follows the Post/Redirect/Get pattern so if you refreshed the page it wouldn't try to re-submit the form again.

这篇关于,同样的形式更新的标签MVC 5按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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