locked
Send dropdownlist selected ID and other detail to Controller action RRS feed

  • Question

  • User-1881265220 posted

    Model

    public class Company

    {

    public int VideoId { get; set; }

    public string VideoName { get; set; }

    }

    public class CustomerModel

    {

    public IEnumerable<company> Videos { get; set; } </company>

    <company>public int SelectedVideoId { get; set; } </company>

    <company>public string SelectedVideoName { get; set; } </company>

    <company>}</company>

    Controller

    public ActionResult Index()

    {

    var videoModel = new VideoModel

    {

    Videos = new List<Video> { new Video { VideoId = 1, VideoName = "Microsoft" },
    new Video { VideoId = 2, VideoName = "IBM" } }

    };

    return View(videoModel);

    }

    [HttpPost]

    public ActionResult Index(VideoModel model)

    {

    int selectedId = model.SelectedVideoId;
    return View(model);

    }

    View

    @model Proxy2.Models.VideoModel

    @using (Html.BeginForm(null, null, FormMethod.Post))

    {

    @Html.DropDownListFor(Model => Model.SelectedVideoId, new SelectList(Model.Videos, "VideoId", "VideoName"));

    <input type="submit" value="submit" />

    @Html.ActionLink("HD", "ChangeQuality", "Home", new { HD = "Yes" }, null)
    @Html.ActionLink("LOW", "ChangeQuality", "Home", new { HD = "No" }, null)

    }

    On submit button click, in [HttpPost] method, I get the SelectedVideoId.

    Question:

    I have 2 more button in the view - you can see @Html.ActionLink. On click of these buttong, say when user click "HD", I need to send 2 values to the controller - "Yes" (telling show HD video) and SelectedVideoId.

    I tried hard, but could not find any solution. Can someone please help. I hope I was able to explain my question.

    Thanks & regards

    Saturday, September 5, 2020 4:28 PM

All replies

  • User-474980206 posted

    Action links render an anchor, which does not include any form data. You can write JavaScript to change the anchor href to include the addition data, or change the links to submit buttons with a name  and a value so you which was clicked. 

    Saturday, September 5, 2020 8:23 PM
  • User-2054057000 posted

    The "SelectedVideoId" should be in the controller to get the Dropdownlist value.

    public ActionResult Index(string SelectedVideoId)
    {
        int selectedId = SelectedVideoId;
        //....
    }

    This is the concept of model binding technique. You can learn more at Model Binding for Simple Types.

    Question:

    I have 2 more button in the view - you can see @Html.ActionLink. On click of these buttong, say when user click "HD", I need to send 2 values to the controller - "Yes" (telling show HD video) and SelectedVideoId.

    @Html.ActionLink will create anchors and not button. You have to correct the codes.

    After that you will have to add 2 new parameters (for the names of the 2 buttons) to your action. This will provide you with the value of the button clicked by the user. 

    Just put the breakpoint on the action and check its value after any of the button is clicked. 

    Thank you !

    Sunday, September 6, 2020 4:44 AM
  • User1312693872 posted

    Hi, makmohan

    makmohan

    I need to send 2 values to the controller - "Yes" (telling show HD video) and SelectedVideoId.

    Use javascript can be easier.

    You can check my demo:

    View:

    @model VideoModel
    @using (Html.BeginForm(null, null, FormMethod.Post))
    {
    
        @Html.DropDownListFor(Model => Model.SelectedVideoId, new SelectList(Model.Videos, "VideoId", "VideoName"), new { id = "SID" });
    
        <input type="submit" value="submit" />
        @Html.ActionLink("HD ", "ChangeQuality", "Home", null, new { onclick = "Submit(this);" })
        @Html.ActionLink("LOW", "ChangeQuality", "Home", null, new { onclick = "Submit(this);" }) 
    }
    
    @section Scripts
        {
            <script type="text/javascript">
    function Submit(link) {
    event.preventDefault();
    if ($(link).attr("id") == "HD")
    { var judge = "Yes"; }
    else
    { var judge = "No"; }
    var value = $("#SID").val();
    var newUrl = "@Url.Action("ChangeQuality", "Home")" + "?HD=" + judge + "&SelectedVideoId=" + value;
    window.location.href = newUrl;
    };
    </script> }

    Controller:

    public ActionResult ChangeQuality(string HD, int SelectedVideoId)
            {
                return RedirectToAction("index");
            }

    Model:

    public class VideoModel
        {
            public IEnumerable Videos { get; set; }
            public int SelectedVideoId { get; set; }
            public string SelectedVideoName { get; set; }
        }
        public class Video
        {
            public int VideoId { get; set; }
            public string VideoName { get; set; }
        }

    Result:

    Best Regards,

    Jerry Cai

    Monday, September 7, 2020 5:45 AM