如何在ASP.NET MVC 4 HTML5表单操作链接到控制器的ActionResult方法 [英] How to link HTML5 form action to Controller ActionResult method in ASP.NET MVC 4

查看:112
本文介绍了如何在ASP.NET MVC 4 HTML5表单操作链接到控制器的ActionResult方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想通过调用视图的关联控制器的的ActionResult 的方法来处理表单内按钮的基本形式类。这里是以下HTML5 code为以下形式:

 < H2>欢迎及LT; / H>< D​​IV>    < H3>登录和LT; / H3 GT&;    <形式方法=邮报行动=<! - 发生的事情在这里 - > >
        用户名:其中;输入类型=文本名称=用户名/> < BR />
        密码:LT;输入类型=文本名称=密码/> < BR />
        <输入类型=提交值=登陆GT&;
        <输入类型=提交值=创建帐户/>
    < /表及GT;< / DIV><! - 更多code ... - >

对应的控制器 code是以下内容:

  [HttpPost]
公众的ActionResult MyAction(字符串输入,采集的FormCollection)
{
    开关(输入)
    {
        案登录:
            //做一些东西...
            打破;
        案创建帐户
            //做一些其他的东西...
            打破;
    }    返回查看();
}


解决方案

您使用的HTML帮助,并有

  @using(Html.BeginForm())
    {
        用户名:其中;输入类型=文本名称=用户名/> < BR />
        密码:LT;输入类型=文本名称=密码/> < BR />
        <输入类型=提交值=登陆GT&;
        <输入类型=提交值=创建帐户/>
    }

或使用URL帮手

 <形式方法=邮报行动=@ Url.Action(MyAction,myController的)>

Html.BeginForm 有几个(13)覆盖在那里你可以指定更多的信息,例如,正常使用时,使用文件上传:

  @using(Html.BeginForm(myaction,myController的,FormMethod.Post,新{ENCTYPE =的multipart / form-data的}))
{
    < ...>
}


如果您不指定任何参数, Html.BeginForm()将创建一个 POST 的形式, 指向当前的控制器和动作电流即可。举个例子,假设你有一个名为控制器帖子,并呼吁行动删除

 公众的ActionResult删除(INT ID)
{
   VAR模型= db.GetPostById(ID);
   返回查看(模型);
}[HttpPost]
公众的ActionResult删除(INT ID)
{
    VAR模型= db.GetPostById(ID);
    如果(型号!= NULL)
        db.DeletePost(ID);    返回RedirectToView(「指数」);
}

和你的HTML页面会是这样:

 < H2>的你确定要删除<?/ H2>
< P>该帖子命名为<强> @ Model.Title< / STRONG>将被删除&下; / P>@using(Html.BeginForm())
{
    <输入类型=提交级=BTN BTN-危险值=删除邮报/>
    <文字和GT;或< /文字和GT;
    @ Url.ActionLink(走地名录,索引)
}

I have a basic form for which I want to handle buttons inside the form by calling the ActionResult method in the View's associated Controller class. Here is the following HTML5 code for the form:

<h2>Welcome</h2>

<div>

    <h3>Login</h3>

    <form method="post" action= <!-- what goes here --> >
        Username: <input type="text" name="username" /> <br />
        Password: <input type="text" name="password" /> <br />
        <input type="submit" value="Login">
        <input type="submit" value="Create Account"/>
    </form>

</div>

<!-- more code ... -->

The corresponding Controller code is the following:

[HttpPost]
public ActionResult MyAction(string input, FormCollection collection)
{
    switch (input)
    {
        case "Login":
            // do some stuff...
            break;
        case "Create Account"
            // do some other stuff...
            break;
    }

    return View();
}

解决方案

you make the use of the HTML Helper and have

    @using(Html.BeginForm())
    {
        Username: <input type="text" name="username" /> <br />
        Password: <input type="text" name="password" /> <br />
        <input type="submit" value="Login">
        <input type="submit" value="Create Account"/>
    }

or use the Url helper

<form method="post" action="@Url.Action("MyAction", "MyController")" >

Html.BeginForm has several (13) overrides where you can specify more information, for example, a normal use when uploading files is using:

@using(Html.BeginForm("myaction", "mycontroller", FormMethod.Post, new {enctype = "multipart/form-data"}))
{
    < ... >
}


If you don't specify any arguments, the Html.BeginForm() will create a POST form that points to your current controller and current action. As an example, let's say you have a controller called Posts and an action called Delete

public ActionResult Delete(int id)
{
   var model = db.GetPostById(id);
   return View(model);
}

[HttpPost]
public ActionResult Delete(int id)
{
    var model = db.GetPostById(id);
    if(model != null) 
        db.DeletePost(id);

    return RedirectToView("Index");
}

and your html page would be something like:

<h2>Are you sure you want to delete?</h2>
<p>The Post named <strong>@Model.Title</strong> will be deleted.</p>

@using(Html.BeginForm())
{
    <input type="submit" class="btn btn-danger" value="Delete Post"/>
    <text>or</text>
    @Url.ActionLink("go to list", "Index")
}

这篇关于如何在ASP.NET MVC 4 HTML5表单操作链接到控制器的ActionResult方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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