-2

I am new to ASP.NET MVC4 With Entity Framework. I just want to pass the value from View to Controller, I am unable to get it. Please help me to get it. Thanks in advance.

This is my View code:

 <form id="changepassword" method="post" action="@Url.Action("Change", "ChangePassword")">
             <input type="password" value="currentpswd" class="form-control pword" placeholder="Current Password" />
             <input type="password" value="newpswd" class="form-control pword" placeholder="New Password" />
             <input type="password" value="cnfrmpswd"  class="form-control pword" placeholder="Confirm Password" />
             <button class="btn btn-success btn-block" value="change">Submit</button>
             </form>



This is my Controller code:

[HttpPost]
        public ActionResult Change(FormCollection forms)
        {
            if (ModelState.IsValid)
            {
              string currentpswd=Convert.ToString(forms["currentpswd"]);
                string newpass= (string)forms["newpswd"];
                string confirmpass=forms["cnfrmpswd"];
                Tbl_Users user = new Tbl_Users();
                if (newpass == confirmpass)
                {
                    user.UserPassword = newpass;
                    db.SaveChanges();
                }
                else
                {

                }

            }
            return RedirectToAction("Index", "ChangePassword");

        }
Vetri
  • 347
  • 2
  • 6
  • 23
  • What is your problem with receiving objects in the controller actions? – Aritra B Feb 26 '14 at 08:46
  • I have no problem with receiving values in Controller. I don't have any field like UserPasswordSalt in my Model and database too, I just want to pass the values from without using Model field in View. – Vetri Feb 26 '14 at 09:06
  • Please share your entire view code and the get and post action results for `ChangePassword` Action. – Aritra B Feb 26 '14 at 09:14
  • Sorry, I didn't write any code for this. My problem is passing the value without using the model field in View like u=>u.UserName. – Vetri Feb 26 '14 at 09:28
  • 2
    Start with [this](http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4). I hope it helps! – Aritra B Feb 26 '14 at 09:36
  • You should not test modelstate isvalid as form collection is a collection rather than a model. – Cookie Feb 27 '14 at 08:37

1 Answers1

0

How about using form collection You would create an action method that will handle the post when the user clicks the post button:

It will post a collect of the form fields to the controller, you can then reference by using the Name property from the original form fields.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection forms)
{
   var userPassword = (string)forms["UserPassword"];
   var userSalt  = (string)forms["UserSalt"]; 
} 
Cookie
  • 594
  • 1
  • 12
  • 34
  • I have tried it, Still unable to get it.And also i tried another one. I have updated my code, Please check it out. – Vetri Feb 27 '14 at 04:49
  • What doesn't work? Do you see an error? When you debug what does the form collection hold? – Cookie Feb 27 '14 at 08:38