Copied to clipboard

Flag this post as spam?

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


  • Mathias valling 16 posts 126 karma points
    Dec 20, 2018 @ 14:48
    Mathias valling
    0

    Get value from input

    Hey all

    Hope someone you code wizards can help me.

    I need to pass some value from my user to a controller.

    <div class="slidecontainer">
            <input type="range" id="sliderValue" min="1" max="100" value="50" class="slider">
    </div>
    

    and then i need a way to pass it as a value in

      using (Html.BeginUmbracoForm<UserProfileSurfaceController>("StoreData", new { @TopicPageId = test }))
        {
            <input type="submit" id="submitAnswar" value="@umbracoHelper.GetDictionaryValue("Next Question")">
        }
    

    i tried with some Javascript / ajax and i was able to pass it to a ApiController. but since this is stainless , i need it in a surfacecontroller.

    really hope someone out there have a magic spell that will work.

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Dec 20, 2018 @ 15:17
    Dan Diplo
    0

    You need your range input to be within the form and it to have a name - it's this what identifies what the value is eg.

    <input type="range" id="sliderValue" min="1" max="100" name="answer" value="50" class="slider">
    

    Look at this tutorial for Surface Controller forms:

    https://our.umbraco.com/Documentation/Reference/Templating/Mvc/Forms/tutorial-partial-views

  • Mathias valling 16 posts 126 karma points
    Dec 20, 2018 @ 15:35
    Mathias valling
    0

    I find whenever i try to pass the Data around like that, my model is always null, when it reach the controller.

    I'm using a UaaS setup so have models / controls in the core.project and a web for my view. not sure if this is having an impact on why my model in the control is null. but found that when using

        using (Html.BeginUmbracoForm<UserProfileSurfaceController>("StoreData", new { @TopicPageId = test }))
        {
            <input type="range" name="sliderName" id="sliderValue" min="1" max="100" value="50" class="slider">
    
            <input type="submit" id="submitAnswar" value="@umbracoHelper.GetDictionaryValue("Next Question")">
    
        }
    

    i'm able to get the to my controller test is just a int = 3000 and i can read that just find in the controller

    so if i'm able to pass the value from my slider input to the int test i should work but i'm stuck at that point

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Dec 21, 2018 @ 10:52
    Dan Diplo
    0

    Strange. Can you create a model class like this:

    public class AnswerModel
    {
        public string SliderName { get; set; }
    }
    

    Then have that as the value to your controller. Then I can suggest is attaching a debugger to the controller and seeing what data is sent.

    Can you post the code for your UserProfileSurfaceController controller?

  • Mathias valling 16 posts 126 karma points
    Dec 21, 2018 @ 11:51
    Mathias valling
    0

    the SurfaceController is using this model

        public class UserProfileModel
    {
        public int TopicPageId { get; set; }
        public int UserId { get; set; }
        public int UserResult { get; set; }
        public int NumberOfQuestions { get; set; }
    
    }
    

    and the controller itself

    using System.Web.Mvc;
    using Umbraco.Core.Models.PublishedContent;
    using Umbraco.Web.Mvc;
    using conservative.Core.Models;
    
    namespace conservative.Core.Controllers
    {
        public class UserProfileSurfaceController : SurfaceController
        {
            UserProfileModel vm = new UserProfileModel();
    
            [HttpPost]
            public ActionResult StoreData(int? UserResult)
            {
                if (UserResult != null)
                {
                    int test2 = vm.UserResult;
                    test2 += (int)TopicPageId;
                    vm.UserResult += test2;
                }
                return RedirectToCurrentUmbracoPage();
            }
        }
    

    When i debug my controller the UserResult is always null so i cannot update the VM with the new values. which i need for later use.

    In other words i need to pull out the value from input ranged. store it in away so i can send it to the surfacecontroller.

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Dec 21, 2018 @ 12:29
    Dan Diplo
    0

    Try changing it to this:

    public ActionResult StoreData(UserProfileModel model)
    {
    }
    

    Then access the values from your model. You can pass primitive types to a controller as it doesn't work with the model binding - you need to pass a class with the properties you want to bind. Your input fields need to have the names match the property names in your class.

  • Mathias valling 16 posts 126 karma points
    Dec 21, 2018 @ 12:42
    Mathias valling
    0

    so fare passing the data from the view like this

      int test = 3000;
    
        using (Html.BeginUmbracoForm<UserProfileSurfaceController>("StoreData", new { UserResult = test }))
        {
            <input type="submit" id="submitAnswar" value="@umbracoHelper.GetDictionaryValue("Next Question")">}
    

    and then using

    public ActionResult StoreData(UserProfileModel model){}
    

    I can store it in the model, but i still have no way of getting the data from the ranged input to the controller.

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Dec 21, 2018 @ 12:49
    Dan Diplo
    100

    On your UserProfileModel you need to have a property called SliderName or whatever name you have given to your slider input.

    public class UserProfileModel
    {
      ....
        public int SliderName { get; set; }
    
    }
    

    This is how the binding works.

  • Mathias valling 16 posts 126 karma points
    Dec 21, 2018 @ 13:06
    Mathias valling
    0

    cheers it got the value from the slider in the model. I'll have to read up on the binding part when i get some free time thanks for the insight

Please Sign in or register to post replies

Write your reply to:

Draft