Why ActionResult method doesn’t take any parameter from Html.BeginForm() inside view?

I tried to make a database named PersonalJobManagement and it includes information of my employees.
I would like to code an ASP.NET MVC website to edit, delete or create new data for this database.

But here is the problem:
I write the create view, I enter parameters like name or id, but parameters’re not processed into the database. There are 8 employees added already and I cannot add the 9th.

My Create Methods in Controller:

public ActionResult Create()
{
     return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Person model)
{
     db.Person.Add(model);
     db.SaveChanges();
     return RedirectToAction("List");
}

My Create View:

       @using (Html.BeginForm())
       {
           @Html.AntiForgeryToken()
           <div>
               <div>
                   <b>Name</b>
                   <div>
                       @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new {} })
                       @Html.ValidationMessageFor(model => model.FirstName, "", new {})
                   </div>
               </div>
               <div>
                   <div>
                       <input type="submit" value="Save it"/>
                   </div>
               </div>
           </div>
       }

And my Person model:

public partial class Person
   {
       public int BusinessEntityID { get; set; }
       public string PersonType { get; set; }
       public string Title { get; set; }
       public string FirstName { get; set; }
       public string MiddleName { get; set; }
       public string LastName { get; set; }
       public System.DateTime ModifiedDate { get; set; }
   
       public virtual BusinessEntity BusinessEntity { get; set; }
   }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

NAILED IT!
My ~/Views/Shared/_Layout.cshtml file has a form tag and i don’t know why but it causes the error when calling POST method. So if you are here, be careful guys


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x