简体   繁体   English

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

[英]Enter data in a view using the dynamic model

In view I want display data from list ucz and insert data to listaOcen. 在视图中,我想显示来自列表ucz的数据并将数据插入到listaOcen。 display the list of ucz works but I have no idea to input data into list 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>
}

Controller: 控制器:

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

It is model. 是模特。

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; }

    }   

Please help :) 请帮忙 :)

Robert, first of all try to avoid dynamic model. 罗伯特首先尝试避免动态模型。 Create a viemodel for your action. 为您的动作创建一个viemodel。 eg 例如

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

        public string FullName { get; set; }

        public double Mark { get; set; }
    }

then fill it in your action 然后填写你的动作

        [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);
    }

after that you can render list of your students and assign them marks and submit it to the server 之后,您可以呈现学生名单并为其分配分数,并将其提交给服务器

@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>
}

after you submit the form, you'll get list of students and their marks on the server 提交表格后,您将在服务器上获得学生列表及其分数

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

that's all. 就这样。 I hope you'll get the point. 希望您能理解。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM