簡體   English   中英

如何在 ASP.Net Core 中將用戶數據保存到 List

[英]How to save user data to List in ASP.Net Core

我們需要在系統中注冊一個新的培訓課程。 將課程名稱、課程價格和課程列表保存到數據庫中。 每門課程的課數不同,所以用戶會動態添加字段來輸入每門課的名稱。 如何從字段中獲取用戶輸入的數據並將它們保存到列表中,然后再保存到數據庫中?

在此處輸入圖片說明

public class RegisterCourseViewModel
    {
        public string Name { get; set; }

        public decimal Price { get; set; }

        public List<Lesson> ListOfLessons { get; set; }
    }
public class Lesson
    {
        public int LessonId { get; set; }
        public string Name { get; set; }
    }
@model RegisterCourseViewModel

<div>
    <h2>Registration a new course</h2>
    <form asp-area="Staff" asp-controller="Course" asp-action="AddCourse" method="post">
        <div asp-validation-summary="All"></div>

        <div>
            <label asp-for="Name"></label>
            <input asp-for="Name" />
            <span asp-validation-for="Name"></span>
        </div>

        <div>
            <label asp-for="Price"></label>
            <input asp-for="Price" />
            <span asp-validation-for="Price"></span>
        </div>
        
        <div ID="items">
            Lesson 1:
            <input type="text" name="item1" size="45"><br>
            <input type="button" value="Add a new lesson" onClick="AddItem();" ID="add">
            <input type="button" value="Delete the lesson" onClick="DeleteItem();" ID="delete">
        </div>

        <div>
            <input type="submit" value="Registration" />
        </div>
    </form>
</div> 

從這個代碼:

        <div>
            <label asp-for="Name"></label>
            <input asp-for="Name" />
            <span asp-validation-for="Name"></span>
        </div>

        <div>
            <label asp-for="Price"></label>
            <input asp-for="Price" />
            <span asp-validation-for="Price"></span>
        </div>

我在控制器方法中獲取 Name 和 Price 並將其保存到 DB。 但是如何獲得用戶輸入的課程名稱列表? 這是控制器的方法:

public IActionResult AddCourse(RegisterCourseViewModel model)
        {
            if (ModelState.IsValid)
            {
                CourseModel newCourse = new CourseModel
                {
                    Name = model.Name,
                    Price = model.Price,
                    ListOfLessons = model.ListOfLessons                <---- How to get this List?
                };

                courseModel.SaveCourse(newCourse);

                return RedirectToAction("Index", "Home", new { area = "Staff" });
            }
            return View(model);
        }
public class CourseModel
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
        public List<Lesson> ListOfLessons { get; set; }
    }

這是一個工作演示:

看法:

@model RegisterCourseViewModel

<div>
    <h2>Registration a new course</h2>
    <form asp-area="Staff" asp-controller="Course" asp-action="AddCourse" method="post">
        <div asp-validation-summary="All"></div>
        //...

        <div ID="items">
            Lesson 1: 
            //change the name here...
            <input type="text" name="ListOfLessons[0].Name"size="45"><br>
            <input type="button" value="Add a new lesson" onClick="AddItem();" ID="add">
            <input type="button" value="Delete the lesson" onClick="DeleteItem();" ID="delete">
        </div>

        <div>
            <input type="submit" value="Registration" />
        </div>
    </form>
</div>
@section Scripts
{
<script>   
    function AddItem() {
        var index = $('input[name^="ListOfLessons"]').length;
        $("#add").before('<input type="text" size="45" name="ListOfLessons[' + index + '].Name" /><br>')           
    }
</script>
}

結果: 在此處輸入圖片說明

通過將課程列表轉換為Json字符串,然后使用數據庫中的一列表保存,就可以得到用戶的動態課程列表。 所以使用數據庫模型,如:

public class RegisterCourse
    {
        public string Name { get; set; }

        public decimal Price { get; set; }

        public string ListOfLessons { get; set; }
    }

使用JsonConvert.SerializeObject(courseList)轉換用戶輸入的課程,然后將其插入數據庫。 JsonConvert.DeserializeObject<Class>(jsonstr)從數據庫模型中讀取用戶的課程列表

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM