Solved

ASP.net MVC passing parameters from view to controller

Posted on 2016-07-15
2
52 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

15 Experts available now in Live!

Get 1:1 Help Now