Url.Action map a wrong link from Route attribute

前端 未结 3 2004
情歌与酒
情歌与酒 2021-01-18 00:15

This is target controller and action:

[RoutePrefix(\"Editor\")]
public class EditorController : Controller

[HttpGet]
    [Route(\"{id:int}\")]
    public A         


        
3条回答
  •  一生所求
    2021-01-18 00:22

    I just faced with same problem. When i fixed the links - editing is broken (form always redirects to the same page).

    Here is solution:

    A link

    @Html.ActionLink("Edit my nice object", "Edit", new { id=item.Id })
    

    A form in the view Edit.cshtml (specifying Controller name is necessary!)

    @using (Html.BeginForm("EditConfirmed", "AppServers"))
    

    The actions in the controller

    public class AppServersController         
        [Route("edit/{id:int?}")]
        public ActionResult Edit(int? id)
        {
            // bla-bla
        }
        [Route("edit_confirmed")]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EditConfirmed([Bind(Exclude = "Created,LastModified")] AppServerVM appServer)
        {
            if (!ModelState.IsValid) return View("Edit", appServer);
            // bla-bla
        }
    }
    

    Now both links and editing works.

提交回复
热议问题