控制器中带有操作的 GET 和 POST - VB.NET 2012 MVC 4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13442055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:11:59  来源:igfitidea点击:

The GET and POST with an Action in the Controller - VB.NET 2012 MVC 4

.netvb.netasp.net-mvc-4

提问by user1799026

EDIT:

编辑:

The function "TheColorNameFunction"is working successfully. The question is, how to have the return View()the function has return back to the "TheRequest"View.

函数“TheColorNameFunction”运行成功。问题是,如何让return View()函数返回到"TheRequest"View

SOLVED:

解决了:

It simply took writing the Viewname into the return View()as following: return View("TheRequest")

只需将View名称写入return View()如下:return View("TheRequest")



The Controller:

控制器:

Function TheRequest() As ActionResult

        Return View()
    End Function


    <HttpPost()>
    Function TheColorNameFunction() As ActionResult

        Response.Write("The color you have submitted: " & Request.Form("ColorName"))

        Return View()
    End Function

The HTML:

HTML:

@Html.BeginForm("TheColorNameFunction", "Home", method:= FormMethod.Post)

<fieldset>
    <input type="text" name="ColorName" />
    <input type="submit" name="ColorName_SubmitButton" />
</fieldset>

The HTML Second:

HTML第二:

            <ul id="menu">
                <li>@Html.ActionLink("Home", "Index", "Home")</li>
                <li>@Html.ActionLink("About", "About", "Home")</li>
                <li>@Html.ActionLink("The Request View", "TheRequest", "Home")</li>

            </ul>

回答by HTX9

If I'm understanding your question correctly, this can be accomplished by either using ViewData or a view model. ViewData is simpler to use in your situation.

如果我正确理解您的问题,这可以通过使用 ViewData 或视图模型来完成。ViewData 在您的情况下更易于使用。

Using ViewData:

使用视图数据:

Your action method:

你的行动方法:

<HttpPost()>
Function TheColorNameFunction(ColorName as String) As ActionResult
    ViewData("message") = "The color you have submitted: " & ColorName
    Return View()
End Function

And in your view you can display the message like this:

在您的视图中,您可以显示如下消息:

@ViewData("message")

UPDATE:Alright, so if I understand correctly you just want to return back to the original view titled "TheRequest", correct? There are two ways you can do that... instead of return View()you can just do return TheRequest(), or if the name of the .vbhtml file is 'TheRequest', you can use return View("TheRequest").

更新:好的,所以如果我理解正确,您只想返回到标题为“TheRequest”的原始视图,对吗?有两种方法可以做到这一点......而不是return View()你可以只做return TheRequest(),或者如果 .vbhtml 文件的名称是“TheRequest”,你可以使用return View("TheRequest").

回答by Jorge Alvarado

I do't know if you are asking how to generate the url of your action, which is explained here: Url.Action parameters?

不知道你是不是问怎么生成你的action的url,这里有解释:Url.Action参数?

or if you just want to submit it, in that case you only need:

或者如果您只想提交它,在这种情况下,您只需要:

<input type=submit value="submit" />

you already have your form to post to your action, so a submit button is more than enough.

您已经有了可以发布到您的操作的表单,因此提交按钮已绰绰有余。

Did I understood your question correctly?

我是否正确理解了您的问题?

UPDATE: For the values to be passed just pass an argument in your action like this:

更新:对于要传递的值,只需在您的操作中传递一个参数,如下所示:

    <HttpPost()>
    Function TheColorNameFunction(colorName as String) As ActionResult

The mechanisms from MVC are smart enough to build a complex object from any field or value that you have in your form

MVC 的机制足够智能,可以从表单中的任何字段或值构建复杂的对象

Your action should be written like this:

你的动作应该这样写:

 @Url.Action("TheColorNameFunction", "Home", "green")