如何在MVC中调用post方法 [英] How can I call a post method in MVC

查看:121
本文介绍了如何在MVC中调用post方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mvc中调用post方法有问题



我尝试过的方法:



这里我的表格

I have problem on calling post method in mvc

What I have tried:

Here my form

<form  method="post">
	<div class="row form-group">
		<div class="col-md-6">
			<input type="text" id="txtfname" class="form-control" placeholder="Your firstname">
		</div>
	</div>
	<div class="row form-group">
		<div class="col-md-6">
			<input type="text" id="lname" class="form-control" placeholder="Your lastname">
		</div>
	</div>

	<div class="row form-group">
		<div class="col-md-12">
			<input type="text" id="email" class="form-control" placeholder="Your email address">
		</div>
	</div>

	<div class="row form-group">
		<div class="col-md-12">
			<input type="text" id="subject" class="form-control" placeholder="Your subject of this message">
		</div>
	</div>

	<div class="row form-group">
		<div class="col-md-12">
			<textarea name="message" id="message" cols="30" rows="10" class="form-control" placeholder="Say something about us"></textarea>
		</div>
	</div>
	<div class="form-group">
		<input type="submit" value="Send Message" class="btn btn-primary" >
	</div>

</form>

推荐答案

这不是你在MVC中做表格的方式。以下是的示例。微软的在线文档 [ ^ ]:



This is not how you do Forms in MVC. Below is an example from Microsoft's online documentation[^]:

@using (Html.BeginForm())
{
    @Html.TextBox("Name");
    @Html.Password("Password");
    <input type="submit" value="Sign In">
}





这是一篇博客文章,详细介绍:使用Razor构建ASP.NET MVC表单(ASP.NET MVC基础系列) - Michael Kennedy on Technology [ ^ ]



这是微软的MVC的官方学习资料: ASP.NET MVC | ASP.NET站点 [ ^ ]

< br>

使用 HTML Helper URLHelper 在MVC中调用post方法。

HTML Helper

@using(Html.BeginForm())

{用户名:< input type =textname =FirstName> < br>

密码:< input type =textname =LastName> < br>



< input type =submitvalue =发送消息>

}

如果你没有在开始表单中指定任何参数,那么它将创建一个对默认控制器的发布请求,该请求映射到从中返回视图的当前操作视图,如果你有不同的名称,即你当前视图是通过控制器的不同操作方法返回,然后你需要在 BeginForm 的参数内提及相同的内容。

@using(Html.BeginForm(actionMethodName,ControllerName, FormMethod.Post ,new {enctype =multipart / form-data})) />
{< ..代码在这里..>}

使用 Url helper
Use HTML Helper or URLHelper to call post method in MVC.
HTML Helper
@using(Html.BeginForm())
{ Username: <input type="text" name="FirstName"> <br>
Password: <input type="text" name="LastName"> <br>
.
<input type="submit" value="Send Message">
}
If you are not specifying any parameter inside begin form then it will create a post request to default controller that is mapped to your current Action from which view is returned view and In case if you have different name i.e.your current view is returned by a different action method of a controller then you need to mention the same inside the parameter of BeginForm ie.
@using(Html.BeginForm("actionMethodName", "ControllerName", FormMethod.Post, new {enctype = "multipart/form-data"}))
{<..Code here..>}
using Url helper


这篇关于如何在MVC中调用post方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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