Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.NETMVC中将数据移动到@helper_C#_Asp.net Mvc_Razor_Html Helper - Fatal编程技术网

C# 如何在ASP.NETMVC中将数据移动到@helper

C# 如何在ASP.NETMVC中将数据移动到@helper,c#,asp.net-mvc,razor,html-helper,C#,Asp.net Mvc,Razor,Html Helper,我想知道如何在MVC中将我的“年数”计算转移到@helper 我想搬家 var years=Enumerable.Range(DateTime.Now.Year-99100).Reverse() 我不确定的一件事是如何移动设置视图模型“年”的代码,这是一个 IEnumerable<SelectListItem> 你为什么想要/需要这样做?从@Html.DropDownListFor()`中使用的值创建SelectList,这应该在控制器上完成,而不是在视图上完成(可以是Years=

我想知道如何在MVC中将我的“年数”计算转移到@helper

我想搬家

var years=Enumerable.Range(DateTime.Now.Year-99100).Reverse()

我不确定的一件事是如何移动设置视图模型“年”的代码,这是一个

IEnumerable<SelectListItem>

你为什么想要/需要这样做?从@Html.DropDownListFor()`中使用的值创建
SelectList
,这应该在控制器上完成,而不是在视图上完成(可以是
Years=new SelectList(Years),
这是我在另一篇博文中提出的建议。我想我的想法是将它从控制器中取出,因为它不应该在那里?然后将它放入助手中,因为它被执行了不止一次。嗨,斯蒂芬,你知道有谁可以帮我完成我正在进行的项目吗?我会支付/hr。我需要javascript和aja方面的帮助我知道这个问题(我自己做了一些评论)它应该在控制器中,但在私有方法中,因为它将被多次调用-在Create和Edit GET方法中,如果需要返回view-
private void ConfigureViewModel(EditTeacherViewModel)的话,在POST方法中再次调用{var years=Enumerable.Range(DateTime.Now.Year-99100).Reverse();model.years=years=new SelectList(years);}
然后在每个方法中,初始化模型后,
配置视图模型(model);
    [HttpGet]
    public ActionResult Edit()
    {
        var years = Enumerable.Range(DateTime.Now.Year - 99, 100).Reverse();
        string userId = User.Identity.GetUserId();
        Student student = studentRepository.Find(userId);

        if (student == null)
        {
            var emptyViewModel = new EditStudentViewModel
            {
                Years = years.Select(c => new SelectListItem
                {
                    Text = c.ToString(),
                    Value = c.ToString(),
                    Selected = (c == 0)
                }),
                AspNetUserRefId = userId,
                StudentImageResult = new FileContentResult(System.IO.File.ReadAllBytes(System.IO.Path.GetFullPath(Server.MapPath("~/App_Data/Images/no_image.jpg"))), "image/jpeg")
            };
            return View(emptyViewModel);
        }

        var studentViewModel = new EditStudentViewModel
        {
            CatchPhrase = student.CatchPhrase,
            SelectedYearId = student.StartedPracticing,
            Years = years.Select(c => new SelectListItem
            {
                Text = c.ToString(),
                Value = c.ToString(),
                Selected = (c == student.StartedPracticing)
            }),
            Location = student.Location,
            Education = student.Education,
            Work = student.Work,
            WhyIPractice = student.WhyIPractice,
            Inspirations = student.Inspirations,
            AspNetUserRefId = student.AspNetUserRefId,
            StudentId = student.StudentId,
            StudentImageResult = new FileContentResult(student.StudentImage.Image, "image/jpeg")
        };

        return View(studentViewModel);
    }