?
Solved

ASP.net MVC passing parameters from view to controller

Posted on 2016-07-15
2
Medium Priority
?
89 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 2000 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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

764 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