Solved

ASP.net MVC passing parameters from view to controller

Posted on 2016-07-15
2
62 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
MediaHelp 4 21
Modal Popup Extender control 1 18
Getting error in connectionstring with Excel. 30 31
VB.NET 2008 Publish Error 2 25
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

785 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