未在Post上设置ASP.NET MVC表单值

未在Post上设置ASP.NET MVC表单值,asp.net,asp.net-mvc-2,Asp.net,Asp.net Mvc 2,我有一个名为Problem的模型对象: [Table(Name = "Problems")] public class Problem { [HiddenInput(DisplayValue = false)] [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)] public int ProblemId { get; set; } [Display(R

我有一个名为Problem的模型对象:

[Table(Name = "Problems")]
public class Problem
{
    [HiddenInput(DisplayValue = false)]
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public int ProblemId { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TablePersonStudentName")]
    [Column] public int StudentId { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TableCommunicationTypesName")]
    [Column] public int CommunicationTypeId { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TableProblemTypeName")]
    [Column] public int ProblemTypeId { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TableMitigatingCircumstanceLevelName")]
    [Column] public int MitigatingCircumstanceLevelId { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TableProblemDate")]
    [Column] public DateTime? DateTime { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TableProblemOutline")]
    [Column] public string Outline { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TableProblemFile")]
    [Column] public byte[] MitigatingCircumstanceFile { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TableProblemAbsentFrom")]
    [Column] public DateTime? AbsentFrom { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TableProblemAbsentUntil")]
    [Column] public DateTime? AbsentUntil { get; set; }

    [Display(ResourceType = typeof(Resources.Resources), Name = "TableProblemRequestedFollowUp")]
    [Column] public DateTime? RequestedFollowUp { get; set; }

    public CommunicationType CommunicationType { get; set; }

    public MitigatingCircumstanceLevel MitigatingCircumstanceLevel { get; set; }

    public ProblemType ProblemCategory { get; set; }

    public ICollection<ProblemCommunication> ProblemCommunications { get; set; }

    public ICollection<AssessmentExtension> AssessmentExtensions { get; set; }

    public ICollection<User> Users { get; set; }

}
这是在导航到问题/创建控制器方法时生成的:

public ViewResult Create()
    {
        string username = User.Identity.Name;

        return View("Edit", new ProblemViewModel(new Problem(), sqlStudentRepository, 
            sqlCommunicationTypeRepository, sqlMitigatingCircumstanceRepository,
            sqlProblemTypeRepository, sqlUserRepository, username));
    }
以下是ascx视图:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<BournemouthUniversity.WebUI.Models.ProblemViewModel>" %>

如果您对此有任何想法,我将不胜感激。在我的大多数表单中,我都有下拉列表,但我不明白为什么所有的值都是空的,即使是非下拉字段

先谢谢你


Jonathan我认为您需要将编辑操作的签名更改为

[HttpPost]
public ActionResult Edit(int problemId, Problem problemValues)
{
.
.
}

看起来你和这里提到的问题一样

如果你检查生成的html,我想你会发现表单字段使用了点符号..基本上是发回模型。问题而不仅仅是问题。。。这就是你的问题

编辑
我没有很好地解释这一点,我认为…您的html字段应该发回属性,这些属性将映射到操作接受的模型,在这种情况下,该操作需要一个问题模型……但是您的html字段正在发回一个有问题的模型……而不是一个问题。

我建议您将存储库与模型分开。这样,所有传递到视图的都是模型。视图和ViewModel都不需要任何存储库。其工作方式是控制器使用存储库获取模型并将此模型传递给视图:

public ViewResult Create()
{
    string username = User.Identity.Name;
    Problem model = someRepository.FetchModel(username);
    ProblemViewModel viewModel = someMapper.ConvertToViewModel(model);

    return View("Edit", viewModel);
}
提交行动:

[HttpPost]
public ViewResult Create(ProblemViewModel viewModel)
{
    // viewModel will contain all the fields that you have corresponding
    // inputs in the View
    ...
}

在深入挖掘之前,我注意到您正在将回购协议传递到viewmodel中……这是一个代码,谢谢您的建议。我对ASP.NETMVC还很陌生。我刚想出一个解决办法让它工作。我想我应该在控制器中填充SelectList所需的列表,然后将它们传递到viewModel。我检查了我的HTML,我认为我没有模型。问题:学生都有问题。相反,这是你的问题。为了正确映射,这些字段应该是ProblemId、StudentId、CommunicationTypeId等,而不是Problem.xxxx解决方案是使用其他Htmlhelper重载,因此插入model.Problem.ProblemId)%>谢谢您的帮助。虽然Darin Dimitrov的解决方案确实有效,但将视图模型发送到视图要容易得多,但在视图中使用您的解决方案可以返回问题对象,而不是视图模型对象。所以我采纳了你们两位的建议。我已经从viewmodels中取出了存储库,现在在调用viewmodel构造函数之后在控制器中获取数据。谢谢你的建议,谢谢!指出我可以只看html就找到了正确的解决方案(facepalm moment)。如果我将控制器操作标题更改为[HttpPost]public ViewResult Edit(ProblemViewModel viewModel),我会得到:没有为此对象定义无参数构造函数。是的,这是正常的。您必须删除接受所有存储库的构造函数,因为它不再有任何意义。虽然这确实提供了“重写所有内容使其更像X”的解决方案,但这并不是您发布的问题的解决方案…感谢您的帮助。此解决方案确实有效,但我发现将视图模型发送到视图更容易,但在视图中使用E Rolnicki解决方案将问题对象返回到编辑操作,而不是viewmodel对象。我已经从viewmodels中取出了存储库,现在在调用viewmodel构造函数之后在控制器中获取数据。谢谢你的建议,谢谢@E Rolnicki,所以不仅仅是为了给出问题的解决方案,它是为了提倡好的实践,将存储库传递给视图,并且使用非强类型的助手绝对不是一个好的实践。
[HttpPost]
public ActionResult Edit(int problemId, Problem problemValues)
{
.
.
}
public ViewResult Create()
{
    string username = User.Identity.Name;
    Problem model = someRepository.FetchModel(username);
    ProblemViewModel viewModel = someMapper.ConvertToViewModel(model);

    return View("Edit", viewModel);
}
[HttpPost]
public ViewResult Create(ProblemViewModel viewModel)
{
    // viewModel will contain all the fields that you have corresponding
    // inputs in the View
    ...
}