Copied to clipboard

Flag this post as spam?

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


  • Tom Engan 430 posts 1173 karma points
    Aug 07, 2017 @ 11:37
    Tom Engan
    1

    Passing model from view to surfacecontroller

    Partial view inside of @using (Html.BeginUmbracoForm

    @foreach (var hikingdestination in Model)
    {
        <tr>
            <!-- UPDATE -->
            <td><a href="/[email protected]"><span class="fa fa-pencil-square-o"></span></a></td>
    
            <!-- DELETE -->
            <td><a href="@Url.SurfaceAction("DeleteHikingDestination", "HikingDestinationSurface", new { Id = @hikingdestination.Id, redirectId = 1165 })"><span class="fa fa-trash-o"></span></a></td>
    
            <td><a href="@Html.DisplayFor(m => @hikingdestination.HikingDestination.Url)">@Html.DisplayFor(m => @hikingdestination.HikingDestination.Name)</a></td>
            <td>@Html.DisplayFor(m => @hikingdestination.StartDate)</td>
            <td>@Html.DisplayFor(m => @hikingdestination.Title)</td>
            <td>@Html.DisplayFor(m => @hikingdestination.HikingCode)</td>
        </tr>
    }
    

    Update hits this action from another url named "/rediger-turmaal" and works as it should:

    [HttpPost]
    [NotChildAction]
    // Submit button: Partial Views UpdateHikingDestination.cshtml 
    public ActionResult UpdateHikingDestination(HikingDestinationViewModel model)
    {
        // Object reference not set to an instance of an object.
        try
        {
            if (Members.IsLoggedIn() && Members.IsMemberAuthorized(allowTypes: new[] { "hiker" }))
                {
                // Servervalidate fields
                if (model.StartDate.ToString().Length < 1) { ModelState.AddModelError("", "Angi startdato for turmål"); }
    
                if (!ModelState.IsValid)
                {
                    return CurrentUmbracoPage();
                }
    
                //nodeId same value as nodeId in db table cmsMember
                model.nodeId = Members.GetCurrentMemberId();
    
                model.SelectedHikingDestination = model.SelectedHikingDestination;
    
                model.Title = model.Title;
                model.StartDate = model.StartDate;
                model.HikingCode = model.HikingCode;
    
                //Get the Umbraco db
                var db = ApplicationContext.DatabaseContext.Database;
    
                //Update the object to the DB
                db.Update(model, model.Id);
    
                //All done - redirect to the page
                ModelState.Clear();
                return Redirect("/turmaal");
            }
    
            TempData["updateSuccess"] = true;
            return CurrentUmbracoPage();
        }
        catch (NullReferenceException ex)
        {
            // Maybe also servervalidate?
            ModelState.Clear();
            return CurrentUmbracoPage();
        }
    }
    

    But don't know why exactly this action was executed, and how model and model.Id was found.


    Delete hit this action now, and it's no surprice (and it works):

    [HttpGet]
    public ActionResult DeleteHikingDestination(int Id, int redirectId)
    {
        // Object reference not set to an instance of an object.
        try
        {
            if (Members.IsLoggedIn() && Members.IsMemberAuthorized(allowTypes: new[] { "hiker" }))
            {
                HikingDestinations.DeleteById(Id);
            }
            TempData["deleteSuccess"] = true;
            return RedirectToUmbracoPage(redirectId);
        }
    
        catch (NullReferenceException ex)
        {
            ModelState.Clear();
            return CurrentUmbracoPage();
        }
    }
    

    But how can I specify the link to use this action instead (similar to UpdateHikingDestination that works)?

    [HttpPost]
    [NotChildAction]
    // Submit button: Partial Views UpdateHikingDestination.cshtml 
    public ActionResult DeleteHikingDestination(HikingDestinationViewModel model)
    {
        // Object reference not set to an instance of an object.
        try
        {
            if (Members.IsLoggedIn() && Members.IsMemberAuthorized(allowTypes: new[] { "hiker" }))
            {
                //Get the Umbraco db
                var db = ApplicationContext.DatabaseContext.Database;
    
                //Delete the DB-object
                db.Delete(model.Id);
    
                //All done - redirect to the page
                ModelState.Clear();
                return Redirect("/turmaal");
            }
    
            TempData["deleteSuccess"] = true;
            return CurrentUmbracoPage();
        }
        catch (NullReferenceException ex)
        {
            // Maybe also servervalidate?
            ModelState.Clear();
            return CurrentUmbracoPage();
        }
    }
    

    How to send a model from view to controller? With Id I find correct action, but not with model.

    Can I use @Url.SurfaceAction for both change and delete buttons/links?

    I have tried these two, but didn't make them work (how should they be written?):

    public static string SurfaceAction(this UrlHelper url, string action, string controllerName, object additionalRouteVals);
    public static string SurfaceAction<T>(this UrlHelper url, string action, object additionalRouteVals) where T : SurfaceController;
    

    I need to get more control of which action is being taken, also because I need to extend the controller with another delete action to confirm that members really want to delete the item before deleting.

  • David Brendel 786 posts 2949 karma points c-trib
    Aug 07, 2017 @ 13:38
    David Brendel
    0

    Hi Tom,

    just wild guessing to be honest but if this works:

    @Url.SurfaceAction("DeleteHikingDestination", "HikingDestinationSurface", new { Id = @hikingdestination.Id, redirectId = 1165 })
    

    have you tried using:

    @Url.SurfaceAction("DeleteHikingDestination", "HikingDestinationSurface", new { model = yourModel })
    

    maybe that works too?

    Regards David

  • Tom Engan 430 posts 1173 karma points
    Aug 07, 2017 @ 13:44
    Tom Engan
    0

    Hi David.

    Then I got this message: "The parameters dictionary contains a null entry for parameter 'Id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult DeleteHikingDestination(Int32, Int32)' in 'Neoweb.Controllers.HikingDestinationSurfaceController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters"

    So it looks like it hits the same Action as before (I wrote { model = Model }, but that's maybe wrong)?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies