Solved

ASP.net MVC passing parameters from view to controller

Posted on 2016-07-15
2
58 Views
Last Modified: 2016-07-18
Hello, i am trying to move some of my member's properties table to a new create action in the members exams controller.  I am able to import all of the parameters however the default ID from the members table keeps routing to the corresponding ID in the members exam controller instead of creating a new member exam.  I know where my problem it as but don't know how to fix.   I have tried to change in the RouteConfig however the new post will not save.  

View
 <td>
                           
                            @Html.ActionLink("Add Exam", "Create", "members_exams", new { id = item.members_id,
                                                                                          name = item.members_firstname, 
                                                                                          lname = item.members_lastname, 
                                                                                          mname = item.members_middlename, 
                                                                                          ssn = item.members_ssn, 
                                                                                          email = item.members_email, 
                                                                                          address = item.members_address, 
                                                                                          city = item.members_city, 
                                                                                          state = item.members_ssn, 
                                                                                          zip = item.members_zip, 
                                                                                          phone = item.members_phone }, 
                                                                                          new { @class = "btn btn-success" })
                            </td>

Open in new window

 

I know  id = item.members_id is cause the problem but how do I fix it to create a new exam.    I have tried to eliminate the ID and use null after the parameters.  

Controller:
	public ActionResult Create(int? id, string name, string mname, string lname, string ssn, string email, string address, string city, string state, string zip, string phone)
		{
			if (id == null)
			{
				return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
			}
			members_exams members_exams = db.members_exams.Find(id);
			if (members_exams == null)
			{
				return HttpNotFound();
			}		
			members_exams.members_id = id;
			members_exams.members_exams_firstname = name;
			members_exams.members_exams_middlename = mname;
			members_exams.members_exams_lastname = lname;
			members_exams.members_exams_ssn = ssn;
			members_exams.members_exams_email = email;
			members_exams.members_exams_address = address;
			members_exams.members_exams_city = city;
			members_exams.members_exams_state = state;
			members_exams.members_exams_zip = zip;
			members_exams.members_exams_phone = phone;

			ViewBag.exams_id = new SelectList(db.exams, "exams_id", "exams_description");
			ViewBag.members_exams_status_id = new SelectList(db.members_exams_status, "members_exams_status_id", "members_exams_status_description");
			ViewBag.members_exams_types_id = new SelectList(db.members_exams_types, "members_exams_types_id", "members_exams_types_description");
			ViewBag.schools_id = new SelectList(db.schools.OrderBy(x => x.schools_name), "schools_id", "schools_name");
			return View(members_exams);
        }

Open in new window




In a different try I have changed the  route config:

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

			routes.MapRoute(
			   name: "Create",
			   url: "Members_exams/Create",
			   defaults: new { controller = "Members_exams", action = "Create" }
		   );
        }

Open in new window


View - I have taken out the ID parameter:
   @Html.ActionLink("Add Exam", "Create", "members_exams", new { name = item.members_firstname,
                                                                                          lname = item.members_lastname,
                                                                                          mname = item.members_middlename,
                                                                                          ssn = item.members_ssn,
                                                                                          email = item.members_email,
                                                                                          address = item.members_address,
                                                                                          city = item.members_city,
                                                                                          state = item.members_ssn,
                                                                                          zip = item.members_zip,
                                                                                          phone = item.members_phone },
                                                                                          new { @class = "btn btn-success" })
                            </td>[/code]

controller:  
public ActionResult Create(int? id, string name, string mname, string lname, string ssn, string email, string address, string city, string state, string zip, string phone)
		{
			members_exams members_exams = new members_exams();	
			members_exams.members_id = id;
			members_exams.members_exams_firstname = name;
			members_exams.members_exams_middlename = mname;
			members_exams.members_exams_lastname = lname;
			members_exams.members_exams_ssn = ssn;
			members_exams.members_exams_email = email;
			members_exams.members_exams_address = address;
			members_exams.members_exams_city = city;
			members_exams.members_exams_state = state;
			members_exams.members_exams_zip = zip;
			members_exams.members_exams_phone = phone;

			ViewBag.exams_id = new SelectList(db.exams, "exams_id", "exams_description");
			ViewBag.members_exams_status_id = new SelectList(db.members_exams_status, "members_exams_status_id", "members_exams_status_description");
			ViewBag.members_exams_types_id = new SelectList(db.members_exams_types, "members_exams_types_id", "members_exams_types_description");
			ViewBag.schools_id = new SelectList(db.schools.OrderBy(x => x.schools_name), "schools_id", "schools_name");
			return View(members_exams);
        }

Open in new window


Thanks for your help,

Jay
0
Comment
Question by:lovettjay
2 Comments
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 41713656
You do not need to modify routing.
What you need is to use a class as a parameter:
public ActionResult Create(members_exams newEntry)
{
	ViewBag.exams_id = new SelectList(db.exams, "exams_id", "exams_description");
	ViewBag.members_exams_status_id = new SelectList(db.members_exams_status, "members_exams_status_id", "members_exams_status_description");
	ViewBag.members_exams_types_id = new SelectList(db.members_exams_types, "members_exams_types_id", "members_exams_types_description");
	ViewBag.schools_id = new SelectList(db.schools.OrderBy(x => x.schools_name), "schools_id", "schools_name");
	return View(members_exams);
}

Open in new window

and modify your view accordingly.

Having said that, your solution should follow the typical CRUD practices shown here, which for your case could be:
// GET: /CRUD/Create
public ActionResult Create()   
{   
    return View(); //Default to members_exams view  
}   

//   
// POST: /CRUD/Create   

[HttpPost]   
[ValidateAntiForgeryToken]   
public ActionResult Create(members_exams newEntry)   
{   
    if (ModelState.IsValid)   
    {   
        db.members_exams.Add(newEntry); //members_exams is the table to update.  
        db.SaveChanges();   
        return RedirectToAction("Index"); //go back to your main view   
    }   

    return View(newEntry);   
}

Open in new window

0
 

Author Closing Comment

by:lovettjay
ID: 41717410
Thanks Miguel. Works Great! I owe you a drink if you are ever in Kansas City
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Class object 2 26
Interview questions for support of a legacy ASP.NET site 4 39
Web Form VB.Net  import CSV 4 26
Not seen Link button 5 15
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now