使用JQuery从下拉列表中获取选定的值并将其保存在数据库中

问题描述

添加Jquery代码之前,我能够将dropdownlist值保存到数据库

function Cartola() {
  var url = "https://api.cartolafc.globo.com/mercado/destaques";
  var response = UrlFetchApp.fetch(url);
  var data = response.getContentText();
  var result = JSON.parse(data);
  
  var apelido = result.Atleta[0].apelido;
  var foto = result.Atleta[0].foto;
  var clube_nome = result.Atleta[0].clube_nome;
  var posição = result.Atleta[0].posicao;
  
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear()
  var headerRow = ['apelido','foto','clube_nome','posição'];
  sheet.appendRow(headerRow);
  for(var i=0;i<result[0].Atleta;i++){
    var row = [result.Atleta[i].apelido,result.Atleta[i].foto,result.Atleta[i].clube_nome,result.Atleta[i].posicao];
    SpreadsheetApp.getActiveSheet().appendRow(row);
  }
}
我尝试了所有命令

此asp.net后端代码中的空引用错误

  <select asp-for="CategoriesId" id="CategoriesId" class="form-control" asp-items="ViewBag.CategoriesId">
                                        <option value="0">select</option>
                                    </select>
<script>
$(document).ready(function () {
    $("#CarMake").hide();
    $('#CategoriesId').on('change',function () {
        if (this.value == '2')           
        {
            $("#CarMake").show();
        }
        else {
            //$(this).val()
            //$('#CategoriesId').data();
           // $('#CategoriesId').val($(this).val());
            $('#CategoriesId').text();
        }
       
    });
});

解决方法

https://dotnetfiddle.net/nSMhi0

控制器和视图模型

public class MyCategory
{
    public int CategoryId { get; set; }
    public string CategoryName { get; set; }
}
public class FatimaViewModel
{
    public IList<MyCategory> CategoryList { get; set; }
    public int SelectedCategory { get; set; }
}
public class HomeController : Controller
{
    [HttpPost]
    public ActionResult Index17(FatimaViewModel vm)
    {
        //put breakpoint here
        var selectedItem = vm.SelectedCategory;
        FatimaViewModel vmOriginal = PopulateViewModel();
        return View(vmOriginal);
    }
    public ActionResult Index17()
    {
        FatimaViewModel vm = PopulateViewModel();
        return View(vm);
    }

查看

@model WebApplication2.Controllers.FatimaViewModel
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"
            integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
            crossorigin="anonymous"></script>
    <title>Index8</title>
    <script>
        $(document).ready(function () {
            $("#CarMake").hide();
            $('#CategoriesId').on('change',function () {
                if (this.value == '2') {
                    $("#CarMake").show();
                }
                //got rid of unnessary code
            });
        });
    </script>
</head>
<body>
    <div>
        <div>Select cateogry 2 to show text</div>
        <div id="CarMake">This is shown when second category selected</div>
        @using (Html.BeginForm())
        {
            @Html.DropDownListFor(r => r.SelectedCategory,new SelectList(Model.CategoryList,"CategoryId","CategoryName"),"--select--",new { id = "CategoriesId" })
            <input type="submit" value="Go" />
        }

    </div>
</body>
</html>