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

Asked

Viewed 52 times

2

How do I pass the value of the Thumb variable to my controller? The two input fields then pass normally, but the Tumb variable has to pass the video Image Url.

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?");

        });


    });
  • Salvaritenss, is that I duplicated the controller and put an s in this more of the two ways the value of Thumb will not, only the value of the title and video_url, Thumb will always null

1 answer

1

Post via Ajax poulando the expected object, or add an Hidden field that will store the value of 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" />
    }

Js function

var thumb = generateThumb(generateCode(video_url));
document.getElementById("thumb").value = thumb;
  • Value is getting null in the controler, but ajax is getting value

  • The Thumb variable correctly receives the value, but does not pass the controller parameter

  • Already includes Leandro

  • The action is SalvarItenss or SalvarItens? Are you sure you are doing the post via ajax by #bt and not by <input type="submit" value="Cadastrar" />, without popular the Hidden field thumb?

  • Yes, it stops in the ajax debbug and assigns the values of the 3 fields, but in the controller only goes the value of the title and video_url

  • then, update the question with your view. pq in each place says it does a different thing

  • Ready updated your identity now.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.