0

Here Is my Input form and Want to pass the input text value to controller but value still null in to Controller And an other problem is cant hit my Controller action Create when I change the name of my Controller like Index my Action point to this Action But Can't pass value as parameter.What I can do???

@using (Html.BeginForm("Create", "Account", FormMethod.Post, new { @id = "form-ui", @class = "form-horizontal form-label-left", @novalidate = "true", enctype = "multipart/form-data" }))

{
        <div class="form-group">
            <label for="txt_fname">First Name:</label>            
            <input type="text" class="form-control" id="txt_fname">
        </div>
        <div class="form-group">
            <label for="txt_lname">Last Name:</label>
            <input type="text" class="form-control" id="txt_lname">
        </div>
        <div class="form-group">
            <label for="txt_email">Email address:</label>
            <input type="email" class="form-control" id="txt_email">
        </div>
        <div class="form-group">
            <label for="txt_username">User Name:</label>
            <input type="text" class="form-control" id="txt_username">
        </div>
        <div class="form-group">
            <label for="pwd">Password:</label>
            <input type="password" class="form-control" id="pwd">
        </div>
}

My Account Controller is:

public class AccountController : Controller
    {
        // GET: Account
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(string txt_fname,string txt_lname,string txt_email,string txt_username,string txt_pwd)
        {
            AccountVM account = new AccountVM();
            account._FirstName = txt_fname;
            account._LastName = txt_lname;
            account._EmailAdress = txt_email;
            account._UserName = txt_username;
            account._Password = txt_pwd;

            //Insert Value
            account.CreateAccount();
            return View();
        }
    }

1 Answer 1

1

You need to put the name attribute that is used for model binding and posting as key value pair back to the controller action. So, change all your input elements to have name attribute:

<input type="text" class="form-control" id="txt_fname" name="txt_fname">

Note name="txt_fname" , now your controller should have a parameter of name txt_name to get the value in it.

As you have a view model there, you should be posting Model object following way.

Declare on top of your view the model this view can bind to for post and display:

@model YourNameSpace.ViewModels.AccountVM

@using (Html.BeginForm("Create", "Account", FormMethod.Post, new { @id = "form-ui", @class = "form-horizontal form-label-left", @novalidate = "true", enctype = "multipart/form-data" }))

{
        <div class="form-group">
            <label for="txt_fname">First Name:</label>            
            @Html.TextBoxFor(x=>x.txt_fname)
        .......
        ......
        </div>
}

and your action method now will have one parameter only of type AccountVM:

[HttpPost]
public ActionResult Create(AccountVM account)
{
    //Insert Value
    account.CreateAccount();
    return View();
}

you might also add validation before calling CreateAccount like :

   if(ModelState.IsValid)
   {
       //Insert Value
       account.CreateAccount();
       return View();
   }
   else
   {
       return View(account);
   }
Sign up to request clarification or add additional context in comments.

8 Comments

@MuhammadSaqib glad it helped, don't forget to accept the answer :)
May God bless you happy
but 2nd point is not Clear please answer 2nd point when I chang Controller name Index to to Create its not hit Action method????
do you have <input type="submit" value="Create"/> in the form?
Can't hit ActionResult Its Only Change the input button name
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.