Reputation: 353
I have input boxes which I need to get the value from so that later they can be redirected to a specific controller, but I don't know how to access the values. Can someone please give me direction on how to get the values?
<form action="@Url.Action("AddSpecific", "Comment", new { AuthorT ="1", WebSiteT = "1", postIdT = 1, TextT = "1",TitleT = "1" })" method="post">
<h1>
<label for="Title">Title</label>
<input name="Title" id="Title" type="text" required />
</h1>
<h1>
<label for="Text">Text</label>
<input name="Text" id="Text" type="text" required />
</h1>
<h1>
<label for="AuthorSite">AuthorSite</label>
<input name="AuthorSite" id="AuthorSite" required />
</h1>
<h1>
<label for="Author">Author</label>
<input name="Author" id="Author" type="text" required />
</h1>
<h1>a
<label for="IdOfPost">IdOfPost</label>
<input name="IdOfPost" id="IdOfPost" type="number" required />
</h1>
<input type="submit" value="Post comment" />
Upvotes: 0
Views: 1438
Reputation: 2403
I would go with using a model to pass through and utilize as a parameter for your action.
You need to change your form html so you are not sending default values. Change:
<form action="@Url.Action("AddSpecific", "Comment", new { AuthorT ="1", WebSiteT = "1", postIdT = 1, TextT = "1",TitleT = "1" })" method="post">
To:
<form action="@Url.Action("AddSpecific", "Comment")" method="post">
Your MySpecificObject (the param object for your action). Note that the property names match the name attribute of your inputs, this is important:
public class MySpecificObject
{
public string AuthorSite {get;set;}
public string Title {get;set;}
public string Text {get;set;}
public string Author {get;set;{
public string IdOfPost {get;set;}
}
The Update Your CommentController:
public ActionResult AddSpecific(MySpecificObject mySpecificObject)
{
//mySpecificObject.IdOfPost
//do work
}
Additionally, if you decide you would like to do this via JavaScript and Ajax, check this StackOverflow post: Post form data to Controller's action with Ajax
Upvotes: 1
Reputation: 1137
Your input elements should have the same name value as the parameter on the controller
For Instance, if your controller looks like this:
public ActionResult AddSpecific(String AuthorT)
{
//Do Some Actions
return View();
}
You should have a form that has an element with the name "AuthorT":
<form action="@Url.Action("AddSpecific")" method="post">
<h1>
<label for="Title">Title</label>
</h1>
<input name="AuthorT" id="AuthorT" type="text" required />
<input type="submit" value="Post Comment"/>
</form>
See the name value of the input element has the same name as the controller parameter, the controller will pick up this value
Upvotes: 0
Reputation: 11480
When you build your Form with the declared attributes, your in essence indicating where it should attempt to Post. Once you've outlined the said attribute the Submit Button will execute to that location. Model View Controller will perform some of your request automatically, so the Controller your pointing to:
public ActionResult Submit(int id, string name, string email)
{
// Utilize parameters.
}
So if you outline your form fields with a name
that matches a parameter in the Controller, MVC will attempt to correlate said field directly to the Controller. So your current approach may not even need to occur.
Upvotes: 0