6

I'm fairly new to MVC and am struggling with routing when I try to pass multiple parameters via the URL.

From a page with the URL: /PersonCAFDetail/Index/3?memberid=4

...I'm trying to get an Html.ActionLink set to point to the Create action such that the id=3 and the memberid=4.

Having read a number of similar posts it seems that the following should work:

@Html.ActionLink("Create New", "Create", null, new { memberid = "memberid" })

However, this results in a URL being created as follows:

<a href="/PersonCAFDetail/Create/3" memberid="memberid">Create New</a>

I have a route set up as:

    routes.MapRoute(
        name: "PersonCAFDetail",
        url: "PersonCAFDetail/Create/{id}/{memberid}",
        defaults: new { controller = "PersonCAFDetail", action = "Create", id = "@\d+", memberid = @"\d+" }                        
        );

The controller accepts two parameters as follows:

 public ActionResult Create(int id, int memberid)
        {
            int cafID = id;
            int personID = memberid;
            ViewBag.detailTypeID = new SelectList(db.tCAFDetailTypes, "detailTypeID", "detailType");
            ViewBag.cafID = new SelectList(db.tFamilyCAFs, "cafID", "issues");
            ViewBag.personID = new SelectList(db.tPersons, "personID", "forename");
            return View();
        }

Any help appreciated.

-------edit for model----------

namespace WhatWorks.Models
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.ComponentModel.DataAnnotations;

    public partial class tPersonCAFDetail
    {
        [Key, HiddenInput(DisplayValue=false)]
        public int cafID { get; set; }

        [Key, HiddenInput(DisplayValue = false)]
        public int personID { get; set; }

        [Key, HiddenInput(DisplayValue = false)]
        public int detailTypeID { get; set; }

        [Required, DataType(DataType.MultilineText)]
        public string notes { get; set; }


        public string FullName
        {
            get
            {
                return tPerson.forename + " " + tPerson.surname;
            }
        }

        public virtual tCAFDetailType tCAFDetailType { get; set; }
        public virtual tFamilyCAF tFamilyCAF { get; set; }
        public virtual tPerson tPerson { get; set; }
    }
}

4 Answers 4

13

Finally, you need pass two parameters to the view:

Index action:

public ActionResult Index(int id, int memberid)
{
    ...
    ViewBag.cafID = id;
    ViewBag.personID = memberid;
    return View();
}

Index.cshtml

@Html.ActionLink("Create New", "Create", "PersonCAFDetail", new { id=ViewBag.cafID , memberid =ViewBag.personID}, null)

And Check your route syntax... id = @"\d+"

 routes.MapRoute(
    name: "PersonCAFDetail",
    url: "PersonCAFDetail/Create/{id}/{memberid}",
    defaults: new { controller = "PersonCAFDetail", action = "Create", id = @"\d+", memberid = @"\d+" }                        
    );
Sign up to request clarification or add additional context in comments.

9 Comments

I'm trying to pass parameters so the values will vary. I need to know how to pass the parameter values into "id" and "memberid". Should the route syntax not have the IDs set with a digit wildcard?
Ok, please show how you define the model or ViewBag to the view
If use Strongly-typed views, check the first line in your .cshtml... @model "YourViewModel". Or if you send values in ViewBag something like ViewBag.MemberID ?
I've added the model to the question. Does that make things clearer?
Yes, something... Which properties from class tPersonCAFDetail referers to id and MemberId params ?
|
0
Html.ActionLink(string, string, object, object)

..is what you're using. Those parameters are as follows:

Html.ActionLink(<link text>, <action name>, <route values>, <html attributes>

You're placing your data into the attributes parameter, which will naturally make them attributes of your link (instead of them being route values).

Usage example:

@Html.ActionLink("Create new", "Create", new { id = Model.cafID, memberid = Model.personID }, null);

7 Comments

Can I put two parameters into the <route values> and if so, how?
Yes, they are anonymous objects.. exactly the same as the attributes. I'll edit my post.
I'm getting an error on the ViewModel not being part of the current context. Should it be? I'm mainly using scaffolded code with a few tweaks...
No no.. ViewModel was just an example. I assumed that you were passing in a model to the view with the properties..
I've added the model to the question as an edit. I think I'm more out of my depth than I first imagined... id should map to cafID and memberid maps to personID. I thought that was handled by the controller once the parameters had been given values from the URL?
|
0

Cause that your Url.Action not working is that the & char in url is encoded, so you must use

@Html.Raw(Html.ActionLink("Create New", "Create", "PersonCAFDetail", new { id=ViewBag.cafID , memberid =ViewBag.personID}, null))

now, It's working ;)

Comments

0

You should create an ActionLink which has an overloaded method

MvcHtmlString HtmlHelper.ActionLink(
    string linkText, 
    string actionName, 
    string controllerName, 
    object routeValues, 
    object htmlAttributes
)

In your case :

@Html.ActionLink("Create New", "Create", PersonCAFDetail, new { id = "id", memberid = "memberid" })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.