简体   繁体   中英

Problems sending two parameters to mvc4 controller

Im trying to send two parameters to my Chapter controller. And then have the model to do some update, and then return to the view I started from.

What I have for now is this:


Move.cshtml:

<div>
    <a href='/Chapter/Move?chapterId=27&parentId=26'>Move under this</a>
</div>

ChapterController.cs:

public ActionResult Move(int chapterId)
{
    if (chapterId == 0)
    {
        return RedirectToAction("Index", "Home");
    }

    var model = new Chapter { _id = chapterId };

    return View(model);
}

public ActionResult UpdateChapterPosition(int chapterId, int parentId)
{
    Chapter.updateChapterPosition(chapterId, parentId);

    return RedirectToAction("Chapter", "Move", new { chapterId = chapterId });
}

Chapter.cs: //Model

public int _id { get; set; }
public int parentId { get; set; }

public static void updateChapterPosition(int chapterId, int parentId)
{
    var con = new    SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ConnectionString);
    con.Open();

    using (var command = new SqlCommand("update Subjects set fk_subfolderTo=@parent where _id=@id", con))
    {
        command.Parameters.AddWithValue("id", chapterId);
        command.Parameters.AddWithValue("parent", parentId);
        command.ExecuteNonQuery();
    }
}

It makes a reload of the view. but doesn't update the database. And no error occurs.

As everyone else said in the comments, you are not calling the UpdateChapterPosition action. I'd also like to add that instead of using an html anchor tag, try using Html.ActionLink like this:

@Html.ActionLink("Move under this", "UpdateChapterPosition", "Chapter", new { chapterId = 27, parentId = 26 })

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