[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

ASP.net MVC passing parameters from view to controller

Posted on 2016-07-15
2
Medium Priority
?
107 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

656 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