简体   繁体   English

通过 ASP.NET Core 3.1/5.0 MVC 中的脚本使用选定的 ''value='' 填充数据库

[英]Populate a Datebase with the selected ''value='' through a Script in ASP.NET Core 3.1/5.0 MVC

In my View cshtml file I have a code similar to this:在我的 View cshtml 文件中,我有一个类似于以下的代码:

....
<div class="select">
<select name="slct" id="slct">
<option selected disabled>Select 1</option>
<option value=1>Option 1</option>
<option value=2>Option 2</option>
<option value=3>Option 4</option>
....
 </select>
 </div>
....
<div class="select">
<select name="slct" id="slct">
<option selected disabled>Select N</option>
<option value=100>Option 100</option>
<option value=101>Option 101</option>
<option value=102>Option 102</option>
....
 </select>
 </div>
....

Basically an N amount of selects, with N possible choices.基本上有 N 个选择,有 N 个可能的选择。

What I want to achieve is that when I press a button in the View, after choosing N options, an amount of N tables is inserted into the Database, and I want to do this through a Script, something of this kind at the end of the Code in the cshtml file:我想要实现的是,当我在视图中按下一个按钮时,在选择 N 个选项后,将 N 个表的数量插入到数据库中,我想通过一个脚本来做到这一点,这种东西在末尾cshtml 文件中的代码:

<script src="~/assets/js/jquery.min.js"></script>
<script>
    $("something").on('something2', function (e) {
        var.........blablablah
    });
</script>

How can I make a Script that collects all the Values chosen in the selects, and call this via a button correctly?如何制作一个收集选择中选择的所有值的脚本,并通过按钮正确调用它? And what code should I implement in the view's Controller so it receives this data and enters it into the Database using a '_context'?我应该在视图的 Controller 中实现什么代码,以便它接收这些数据并使用“_context”将其输入数据库?

What happens is that I created a Model containing the variables "Option" and "Date", in my View I generate an interface that allows choosing one of those "Option" for a specific date (each date is a Select), and what I want is that when I press the button, this insert into the database X tables of the Model, with the "Option" that I chose for the corresponding Date.发生的事情是,我创建了一个包含变量“选项”和“日期”的 Model,在我的视图中,我生成了一个界面,允许为特定日期选择其中一个“选项”(每个日期都是一个选择),以及我想要的是,当我按下按钮时,使用我为相应日期选择的“选项”插入 Model 的数据库 X 表中。

I hope I have explained it well and I apologize in advance if it wasn't like that.我希望我已经解释得很好,如果不是这样,我提前道歉。 I am new into programming on ASP dot NET and I am having problems understanding how the View Code really communicates with the Controllers, since I am programming my application using Visual Studio 2019, and much of the code is very...automated so to speak, and that confuses me.我是在 ASP dot NET 上编程的新手,我在理解视图代码如何与控制器真正通信时遇到问题,因为我正在使用 Visual Studio 2019 对我的应用程序进行编程,而且大部分代码都非常......可以说是自动化的,这让我很困惑。

The real code of my application is too big and it is private because is for Company that is why I didn't put it, so I felt that presenting my question in this way would be better.我的应用程序的真实代码太大而且它是私有的,因为是针对公司的,这就是我没有放它的原因,所以我觉得以这种方式提出我的问题会更好。

EDIT: Consider that the Model's code is something similar to this.编辑:考虑模型的代码与此类似。

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace Appp.Models
{
    public class OptionDates
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public string Option { get; set; }
        public DateTime Date { get; set; }
    }

What I want to achieve is that when I press a button in the View, after choosing N options我想要实现的是,当我在视图中按下一个按钮时,在选择 N 个选项后

For this requirement,no need to use jquery,just using default form submit like below:对于此要求,无需使用 jquery,只需使用默认表单提交,如下所示:

<form asp-action="Create">
    <div class="select">
        <select name="slct" id="slct">
            <option selected disabled>Select 1</option>
            <option value=1>Option 1</option>
            <option value=2>Option 2</option>
            <option value=3>Option 3</option>
        </select>
    </div>
    <div class="select">
        <select name="slct" id="slct">
            <option selected disabled>Select 2</option>
            <option value=4>Option 4</option>
            <option value=5>Option 5</option>
            <option value=6>Option 6</option>
        </select>
    </div>
    <div class="select">
        <select name="slct" id="slct">
            <option selected disabled>Select 3</option>
            <option value=7>Option 7</option>
            <option value=8>Option 8</option>
            <option value=9>Option 9</option>
        </select>
    </div>
    <input type="submit" value="post" />
</form>

Controller: Controller:

[HttpPost]
public IActionResult Create(List<int> slct)   
{

    return View("Privacy");
}

Result:结果:

在此处输入图像描述

Update:更新:

For how to use jquery to pass selected value list to backend,you could follow:关于如何使用 jquery 将选定的值列表传递给后端,您可以遵循:

 <script>
    $("input[type=button]").click(function () {
        var data = [];
        var elems = document.querySelectorAll('#slct');
        elems.forEach(function (el) {
            data.push(el.options[el.selectedIndex].value)
        })
        $.ajax({
            url: "/Home/Create",
            type: "Post",
            data: { slct: data }
        })
    });
    
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 FFMPEG 的 ASP.NET Core 5.0 MVC HLS 转码 - ASP.NET Core 5.0 MVC HLS Transcoding using FFMPEG 使用 asp.net core 3.1 mvc 的级联下拉列表 - Cascade Dropdownlist using asp.net core 3.1 mvc 在 Asp.net core Mvc 中提交表单后,下拉列表未保留所选值 - Dropdown list is not retaining the selected value after submitting the form in Asp.net core Mvc Asp.net core mvc 将错误的字符串值解析为<script> using Base64 - Asp.net core mvc parsing wrong string value to <script> using Base64 无法将来自 Joke API 服务器的响应渲染到视图 ASP.NET Core 5.0 MVC - Unable to render the response from the Joke API server to the view ASP.NET Core 5.0 MVC Java 脚本模块(导入/导出)与 Asp.net Core Mvc - Java Script Module(Import/Export) with Asp.net Core Mvc 修改后的 js (javascript) 未加载到移动设备 ASP.NET MVC Core 3.1 - Modified js (javascript) not loaded on the mobile device ASP.NET MVC Core 3.1 无法在ASP.NET MVC中的DropDownList中检索选定的值? - Unable to retrieve selected value in DropDownList in ASP.NET MVC? 如何使用 ASP.NET Core 3.1 MVC 中的 ViewModel 与 JavaScript 和 C# 动态添加到列表 - How to dynamically add to list with ViewModel in ASP.NET Core 3.1 MVC with JavaScript and C# 通过JavaScript获取Asp.net中单选按钮的选定值 - Get selected value of radio button in Asp.net through javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM