How do I pass the value of the Tumb variable to my controler?

2

How do I pass the value of the Thumb variable to my control? The two input fields then passing normally, however the tumb variable has to pass the Image Url of the video.

Controller

  [HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult SalvarItens(string titulo, string video_url, 
   string thumb) 
    {
        var item = new Dados_Video() 
        {
            Titulo = titulo,
            Thumb = thumb,
            UrlVideo = video_url
        };

            db.Dados_Video.Add(item);
            db.SaveChanges();

        return RedirectToAction("Index");
    }

My View

  @using (Html.BeginForm("SalvarItens", "Home", FormMethod.Post, new { 
    @class ="form-horizontal", role = "form" }))
        {
            @Html.AntiForgeryToken()
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger"})

           <input type="text" id="titulo" name="titulo"/>
           <input type="url" id="video-url" name="video_url"/>
           <input type="hidden" id="thumb" name="thumb"  />

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

Js function

   function generateCode(url) {
        var video_id = url.split('v=')[1];
        var ampersandPosition = video_id.indexOf('&');
        if (ampersandPosition != -1) {
            video_id = video_id.substring(0, ampersandPosition);
        }
        return video_id
    }

    function generateThumb(code) {
        return 'https://img.youtube.com/vi/' + code + '/1.jpg';
    }


     var thumb = generateThumb(generateCode(video_url));

Ajax Post

   jQuery(document).ready(function () {

        $("#bt").on("click", function (event) {
            debugger;
            if (grecaptcha.getResponse().length !== 0) {

                var video_url = $('#video-url').val();
                var thumb = generateThumb(generateCode(video_url));
                var titulo = $('#titulo').val();

                $.ajax({
                    url: "/Home/SalvarItens",
                    data: { titulo: titulo, video_url: video_url, thumb: 
  thumb },
                    type: "POST",
                    datatype: "Json",
                    success: function (data) {
                        if (data.Resultado > 0) {

                        }
                    }
                });

                alert("Td certo");
            }
            else
                alert("Voce é um robo?");

        });


    });
    
asked by anonymous 19.09.2018 / 21:06

1 answer

1

Make the post via Ajax by scrolling the expected object, or add a hidden field that will store the value of the thumb and popule via JS

View

   @using (Html.BeginForm("SalvarItenss", "Home", FormMethod.Post, new { 
   @class ="form-horizontal", role = "form" }))
    {
        @Html.AntiForgeryToken()
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger"})

       <input type="text" id="titulo" name="titulo"/>
       <input type="url" id="video-url" name="video_url"/>

       <input type="hidden" id="thumb" name="thumb" />

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

Function Js

var thumb = generateThumb(generateCode(video_url));
document.getElementById("thumb").value = thumb;
    
20.09.2018 / 02:33