Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have problem on calling post method in mvc

What I have tried:

Here my form
HTML
<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>
Posted
Updated 6-Sep-17 19:50pm
v2
Comments
Dave Kreskowiak 6-Sep-17 11:49am    
So, uh, yeah. Did you want to describe the problem of just let everyone guess at it?

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

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


Here is a blog article that goes into more detail: Building ASP.NET MVC Forms with Razor (ASP.NET MVC Foundations Series) – Michael Kennedy on Technology[^]

And here is Microsoft's official learning material for MVC: ASP.NET MVC | The ASP.NET Site[^]
 
Share this answer
 
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900