ASP.NET MVC使用两个输入与Html.BeginForm [英] ASP.NET MVC Using two inputs with Html.BeginForm

查看:312
本文介绍了ASP.NET MVC使用两个输入与Html.BeginForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能有两个输入,或按钮控制,势必Html.BeginForm

同一控制器内触发两个不同的动作

在下面的例子中,我要订阅输入呼叫控制器上订阅的行动。这种运作良好。我想退订输入打电话给我退订的动作。我如何做到这一点? TIA。

 <%使用(Html.BeginForm(订阅,新闻报))
    {%GT;
        < D​​IV CLASS =主编场>
            <%= Html.TextBoxFor(型号=> model.EmailAdress)%GT;
            &所述;%= Html.ValidationMessageFor(
                模型=> model.EmailAdress)%GT;
        < / DIV>
        < D​​IV>
            <输入类型=提交级=提交值=订阅/>
            <输入类型=提交级=提交值=取消订阅/>
        < / DIV>
<%}%GT;

更新

只是更多的情况下这个问题:

我NewsLetterController有两个动作订阅和退订如下公布:

 公共类NewsLetterController:控制器
    {
        [HttpPost]
        公众的ActionResult订阅(NewsletterSubscriber订户)
        {
           // 做一点事        }        [HttpPost]
        公众的ActionResult退订(NewsletterSubscriber订户)
        {
            // 做一点事
        }

我希望我可以调用从同一个表单元素每一个动作,但这显然是假的。我如何依然保持了MVC模式,但是如果有可能避免的脚本?该贴HTML呈现在主页上的局部视图的一部分。

 <%@控制语言=C#继承=System.Web.Mvc.ViewUserControl
                                   < AkwiMemorial.Models.NewsletterSubscriber>中%GT;   < H2><强>通讯< / STRONG>< / H>
    <传奇>输入你的电子邮件< /传说>    <使用%(Html.BeginForm(订阅,新闻报))
       {%GT;
            < D​​IV CLASS =主编场>
                <%= Html.TextBoxFor(型号=> model.EmailAdress)%GT;
                <%= Html.ValidationMessageFor(型号=> model.EmailAdress)%GT;
            < / DIV>
           < D​​IV>
             <输入类型=提交级=提交名称=亚健康
                    值=订阅/>
              <输入类型=提交级=提交名称=混蛋
                     值=退订/>
            < / DIV>
    <%}%GT;


解决方案

这是HTML 格式元素只有一个动作。所以,除非您的按钮更改使用JavaScript,每种形式获取只有一个URI。你想让你的网站使用JavaScript的工作,才会启用?

另一个选择是给这些按钮名称,在这种情况下,你会得到作为部分张贴的形式,例如:

 <输入类型=提交级=提交名称=通讯VALUE =订阅/>
<输入类型=提交级=提交名称=通讯VALUE =退订/>

然后您可以将检查该值,例如一个动作:

 公众的ActionResult订阅(串通讯)
{
    如果(订阅.Equals(通讯,StringComparison.OrdinalIgnoreCase))
    {
        // ...

由于通讯在提交的表单的一个关键,MVC将结合其价值的行动的说法。

Is it possible to have two inputs, or button controls, trigger two different actions within the same controller bound to Html.BeginForm?

In the following example, I want the Subscribe input to call Subscribe action on the controller. This works well. I want the Unsubscribe input to call my Unsubscribe action. How do I make this happen? TIA.

<% using (Html.BeginForm("Subscribe", "NewsLetter"))
    {%>    
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.EmailAdress) %>
            <%= Html.ValidationMessageFor(
                model => model.EmailAdress) %>                                
        </div>                        
        <div >
            <input type="submit" class="submit" value="Subscribe" />
            <input type="submit" class="submit" value="Unsubscribe" />
        </div>         
<% } %>

Update:

Just more context to this question:

My NewsLetterController has two actions Subscribe and Unsubscribe as posted below:

public class NewsLetterController : Controller
    {      
        [HttpPost]
        public ActionResult Subscribe(NewsletterSubscriber subscriber)
        {
           // do something

        }

        [HttpPost]
        public ActionResult Unsubscribe(NewsletterSubscriber subscriber)
        {
            // do something
        }

I was hoping that I can invoke each action from the same Form element, but this apparently is false. How do I still maintain the MVC pattern but avoid scripting if possible? The posted HTML is part of a partial view rendered on the main page.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl
                                   <AkwiMemorial.Models.NewsletterSubscriber>" %>

   <h2><strong>Newsletter</strong></h2>
    <legend>Enter your email</legend>

    <% using (Html.BeginForm("Subscribe", "NewsLetter"))
       {%>    
            <div class="editor-field">
                <%= Html.TextBoxFor(model => model.EmailAdress) %>
                <%= Html.ValidationMessageFor(model => model.EmailAdress) %>
            </div>                        
           <div >
             <input type="submit" class="submit" name="sub" 
                    value="Subscribe" />
              <input type="submit" class="submit" name="unsub" 
                     value="Unsubscribe" />
            </div>         
    <% } %>

解决方案

An HTML form element has only one action. So unless your buttons change that with JavaScript, each form gets only one URI. Do you want your site to work with JavaScript enabled only?

Another option is to give the buttons a name, in which case you'll get the value as part of the posted form, e.g.:

<input type="submit" class="submit" name="newsletter" value="Subscribe" />
<input type="submit" class="submit" name="newsletter" value="Unsubscribe />

Then you can have a single action which checks the value, e.g.:

public ActionResult Subscribe(string newsletter)
{
    if ("subscribe".Equals(newsletter, StringComparison.OrdinalIgnoreCase))
    {
        //...

Because newsletter is a key in the posted form, MVC will bind its value to the action's argument.

这篇关于ASP.NET MVC使用两个输入与Html.BeginForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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