Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing multiple parameters from url to html.actionlink

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; }
    }
}
like image 920
melkisadek Avatar asked Nov 05 '12 04:11

melkisadek


People also ask

How do you pass data from view to controller using ActionLink?

ActionLink is rendered as an HTML Anchor Tag (HyperLink) and hence it produces a GET request to the Controller's Action method which cannot be used to send Model data (object). Hence in order to pass (send) Model data (object) from View to Controller using @Html.

What is ActionLink in HTML?

ActionLink(HtmlHelper, String, String) Returns an anchor element (a element) for the specified link text and action. ActionLink(HtmlHelper, String, String, Object) Returns an anchor element (a element) for the specified link text, action, and route values.


1 Answers

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+" }                        
    );
like image 198
Mate Avatar answered Nov 13 '22 14:11

Mate