Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ben McKean 272 posts 549 karma points
    Aug 13, 2015 @ 21:45
    Ben McKean
    1

    Custom controller with Html.ActionLink issues

    Hi All,

    I'm having some troubles with a custom controller. When I use Html.ActionLink the URL rendered out are and something looks wrong to me:

    <a href="/umbraco/Surface/UserManagerSurface/RenderCreateMember">Create user</a>
    

    and

    <a memberid="1093" href="/umbraco/RenderMvc/RenderEditMember?Length=18">Edit user</a>
    

    My source to render links the links in my View are:

    @Html.ActionLink("Create user", "RenderCreateMember", "UserManagerSurface")
    
    @Html.ActionLink("Edit user", "RenderEditMember", "UserManagerSurface", new { MemberID = 1093 })
    

    Eventually, they should open up forms in a lightbox/popup.

    Here is my surface controller:

     public class UserManagerSurfaceController : SurfaceController
        {
    
            public ActionResult RenderCreateMember()
            {
                return PartialView("_CreateUser", new MemberViewModel());
            }
    
            [HttpPost]
            public ActionResult HandleCreateMember(MemberViewModel model)
            {
                if (!ModelState.IsValid)
                {
                    return PartialView("/_CreateMember", model);
                }
    
                // create member
    
                //Return the view...
                return PartialView("_CreateMember", model);
            }
    
            public ActionResult RenderEditMember(int MemberID)
            {
                //look up member
                Member findMember = Member.GetAllAsList().FirstOrDefault(x => x.Id == MemberID);
    
                MemberViewModel memberViewModel = new MemberViewModel();
    
                if (findMember != null)
                {
                    memberViewModel.EmailAddress = findMember.Email;
                }
                return PartialView("_EditUser", memberViewModel);
            }
    
            [HttpPost]
            public ActionResult HandleEditMember(MemberViewModel model)
            {
                if (!ModelState.IsValid)
                {
                    return PartialView("/_EditUser", model);
                }
    
                // find and update member
    
                //Return the view...
                return PartialView("_EditUser", model);
            }
    

    and here is my partial for _EditUser:

    @model MemberViewModel
    @using (Html.BeginUmbracoForm<UserManagerSurfaceController>("HandleEditMember"))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
    
        <fieldset>
            <legend>Register</legend>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.FirstName)
            </div>
    
            <div class="editor-field">
                @Html.EditorFor(model => model.FirstName)
                @Html.ValidationMessageFor(model => model.FirstName)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.LastName)
            </div>
    
            <div class="editor-field">
                @Html.EditorFor(model => model.LastName)
                @Html.ValidationMessageFor(model => model.LastName)
            </div>
    
    
            <div class="editor-label">
                @Html.LabelFor(model => model.EmailAddress)
            </div>
    
            <div class="editor-field">
                @Html.EditorFor(model => model.EmailAddress)
                @Html.ValidationMessageFor(model => model.EmailAddress)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Password)
            </div>
    
            <div class="editor-field">
                @Html.EditorFor(model => model.Password)
                @Html.ValidationMessageFor(model => model.Password)
            </div>
    
    
            <p>
                <input type="submit" value="Update" />
            </p>
        </fieldset>
    
    }
    

    Clicking on the Edit User link brings up the following error:

    There is not current PublishedContentRequest, it must be initialized before the RenderRouteHandler executes

    I've tried, browsing to ../umbraco/Surface/UserManagerSurface/RenderEditMember?MemberID=1093 and the forms appears as I'd normally expect.

    Can somebody please tell me where I'm going wrong?

    Thanks

  • Ben McKean 272 posts 549 karma points
    Aug 13, 2015 @ 22:03
    Ben McKean
    0

    Also, going to ../umbraco/Surface/UserManagerSurface/RenderEditMember?MemberID=1093, the form post doesn't work. It doesn't hit the controller

  • Ben McKean 272 posts 549 karma points
    Aug 14, 2015 @ 08:20
    Ben McKean
    0

    Ok, so I've got the link working correctly now, I was missing something on my constructor for Html.ActionLink. I've now changed it to the following and it works:

    @Html.ActionLink("Edit user", "RenderEditMember", "UserManagerSurface", new { MemberID = 1093 }, null)
    

    This now takes me to http://localhost:49806/umbraco/Surface/UserManagerSurface/RenderEditMember?MemberID=1093 and displays the form correctly

    but now the form doesn't post. It doesn't hit the HandleEditMember action in my controller when I post the form, it just hits the RenderEditMember action again.

  • Alex 79 posts 416 karma points
    Aug 14, 2015 @ 09:03
    Alex
    0

    Hi,

    Mine is not working

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Aug 14, 2015 @ 10:19
    Ismail Mayat
    0

    Ben,

    I have an umbraco 6 site and for a contact form i have

    @{
    //Client Side Validation
    Html.EnableClientValidation(true);
    Html.EnableUnobtrusiveJavaScript(true);
    

    then further down

    using (Html.BeginUmbracoForm<ContactFormSurfaceController>("HandleContactForm", null, new { id = "contactform" }))
    {
    

    and then the fields then a submit button

    <input type="submit" value="@Dictionary.GetItem("Site.Pages.Contact.Send")" class="btn btn--pink" />
    

    works for me

  • Ben McKean 272 posts 549 karma points
    Aug 14, 2015 @ 10:38
    Ben McKean
    0

    Thanks for the reply Ismail. My code is pretty much the same, I've even removed all validation on the viewmodel and removed the client validation to make it really simple.

    Any post always seems to hit the RenderEditMember method rather than the HandleEditMember

  • Ben McKean 272 posts 549 karma points
    Aug 14, 2015 @ 12:31
Please Sign in or register to post replies

Write your reply to:

Draft