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

时间:2015-07-23 09:22:21

标签: c# asp.net-mvc view

在视图中我希望显示来自列表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; }

    }   

请帮忙:)

1 个答案:

答案 0 :(得分:1)

罗伯特,首先尝试避免动态模型。为您的操作创建一个viemodel。 e.g。

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

全部。我希望你能明白这一点。