简体   繁体   中英

passing id parameter to a controller's create action internally using viewbag

In my model I have a one to many relationship between Users and Topics class. Code for the two classes is

public class Topic
{
    public int TopicID { get; set; }
    public int UserID { get; set; }
    public virtual User User { get; set; } 
    [Display(Name="Topic Subject")]
    public string TopicSubject { get; set; }
    [Display(Name="Date")]
    [DataType(DataType.Date)]
    public DateTime TopicDate { get; set; }
    [DisplayName("Name")]
    public int CatagoryID { get; set; }
    [DisplayName("FullName")]

    public virtual Catagory Catagories { get; set; }

    public virtual ICollection<Reply> Replies { get; set; }
}

and

public class User
{
    public int UserID { get; set; }

    [Display(Name = "First Name")]
    [StringLength(50)]
    public string FirstName { get; set; }

    [Display(Name = "Last Name")]
    [StringLength(50)]
    public string LastName { get; set; }

    [Display(Name="Password")]
    [DataType(DataType.Password)]
    public string UserPass { get; set; }

    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }
    [DataType(DataType.Date)]
    public DateTime Date { get; set; }
    [Display(Name = "Full Name")]
    public string FullName
    {
        get { return FirstName + ", " + LastName; }
    }
    public virtual ICollection<Topic> Topics { get; set; }
    public virtual ICollection<Reply> Replies { get; set; }

}

In my controller the action method I used an int id parameter to pass the id internally from the view

public ActionResult Create(int id)
{
    ViewBag.CatagoryID = id;
    ViewBag.UserID = id;
    return View();
}

But when I try to access Topics/Create in my browser an error like this appears and it's bugging me:

在此处输入图片说明

Your Create method is the HttpGet version - meaning, why are you passing the id for an object that hasn't been created in order to "create" an object? This should be empty for a create, and the HttpPost should pass back the model so it can only then be created.

Try looking at the create mvc example: http://www.asp.net/mvc/overview/older-versions-1/getting-started-with-mvc/getting-started-with-mvc-part6

For the controller to work you need to pass the id in the query parameters. So you need to use the below url.

http://localhost:63952/Topics/Create?id=1

If you want the id to be optional you need to change your action method like below.

public ActionResult Create(int? id)
{
    ViewBag.CatagoryID = id;
    ViewBag.UserID = id;
    return View();
}

The above will tell C# id param is optional and MVC will not expect it to be present on the query parameters

Hope this helps!!

尝试 localhost:63952/Create/5 5 指的是您在函数 Create 中传递的 id 您可以尝试任何可以工作的数字

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM