使用动态模型在视图中输入数据

Enter data in a view using the dynamic model

在视图中,我想显示列表 ucz 中的数据并将数据插入 listaOcen。显示 ucz 作品列表,但我不知道将数据输入列表 listaOcen

@using biblioteka;
@model dynamic

    <tr>

@for (int i = 0; i < Model.dl;i++ )
{

   td>@Model.Uczniowie[i]</td>
<td>
@Html.DropDownList(Model.listaOcen[i], new[] { //doesnt work
       new SelectListItem() { Text = "5", Value = "5" },
       new SelectListItem() { Text = "4+", Value = "4.75" },

}, "Select option")
<td>
</tr>
}

控制器:

        public ActionResult Add(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var dataContext = db.Uczniowie;
            var ucz = dataContext.Where(m => m.KlasaId == id)
                                 .Select(m => m.Nazwisko + " " + m.Imie);


            List<double> listOfOcena = new List<double>();
            dynamic mymodel = new ExpandoObject();
            mymodel.Uczniowie = ucz.ToList();
            mymodel.dl = ucz.ToList().Count;
            mymodel.listaOcen = listOfOcena;// enter the data from DropDownLists
            return View(mymodel);

是模特。

public  class Ocena
    {         
        public string IdUcznia { get; set; }
        public string Id { get; set; }
        public string Przedmiot { get; set; }           
        public double Wartosc { get; set; }
        public double waga { get; set; }
        public DateTime? Data { get; set; }
        public string Rodzaj { get; set; }
        public string Opis { get; set; }

    }   

请帮忙:)

罗伯特,首先尽量避免动态模型。为您的操作创建一个 viemodel。例如

    public class StudentMarkAssignViewModel
    {
        public long StudentId { get; set; }

        public string FullName { get; set; }

        public double Mark { get; set; }
    }

然后将其填写在您的操作中

        [HttpGet]
    public ActionResult Add(string id)
    {
        var model = new StudentMarkAssignViewModel[]
        {
            new StudentMarkAssignViewModel {StudentId = 1, FullName = "Martin", Mark = 0 },
            new StudentMarkAssignViewModel {StudentId = 2, FullName = "Robert", Mark = 5 }
        };
        //here i use hardcoded valuse, but you should get them from your db
        return View(model);
    }

之后你可以渲染你的学生名单并给他们打分并提交给服务器

@using WebApplication100.Models

@model StudentMarkAssignViewModel[]

@using (Html.BeginForm("Add", "StudentMarks", FormMethod.Post))
{
    <table>
        @for (var index = 0; index < Model.Length; index++)
        {
            <tr>
                @Html.HiddenFor(x => Model[index].StudentId)
                @Html.HiddenFor(x => Model[index].FullName)
                <td>@Model[index].FullName</td>
                <td>
                    @Html.DropDownListFor(x => Model[index].Mark, new SelectListItem[]{
                        new SelectListItem{ Value = "0", Text = "Not Rated", Selected = Model[index].Mark == 0  } ,
                        new SelectListItem{ Value = "5", Text = "5"  , Selected = Model[index].Mark == 5  } ,
                        new SelectListItem{ Value = "4.75", Text = "4+"  , Selected = Model[index].Mark == 4.75  }
               })
                </td>
            </tr>
        }
    </table>

    <button type="submit">submit</button>
}

提交表格后,您将在服务器上获得学生名单及其分数

    [HttpPost]
    public ActionResult Add(StudentMarkAssignViewModel[] marksModel)
    {
        /* do whatever you need with your model*/
        return RedirectToAction("Index");
    }

就是这样。我希望你能明白我的意思。