gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:15:20 25 4
gpt4 key购买 nike

我有一个名为 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; }

}

由于此模型包含许多来自其他数据库表的对象,因此我通过使用 viewModel 在我的 View 中使用下拉列表:

public class ProblemViewModel
{
public Problem Problem { get; set; }
public SelectList Students { get; set; }
public SelectList CommunicationType { get; set; }
public SelectList MitigatingCircumstanceLevel { get; set; }
public SelectList ProblemType { get; set; }
public MultiSelectList ProblemUsers { get; set; }

public ProblemViewModel(Problem problem, ISqlStudentRepository sqlStudentRepository,
ISqlCommunicationTypeRepository sqlCommunicationTypeRepository, ISqlMitigatingCircumstanceLevelRepository sqlMitigatingCircumstanceRepository,
ISqlProblemTypeRepository sqlProblemTypeRepository, ISqlUserRepository sqlUserRepository,
string username)
{
this.Problem = problem;
this.Students = new SelectList(sqlStudentRepository.Students.ToList(), "StudentId", "FirstName");
this.CommunicationType = new SelectList(sqlCommunicationTypeRepository.CommunicationTypes.ToList(), "CommunicationTypeId", "Name");
this.MitigatingCircumstanceLevel = new SelectList(sqlMitigatingCircumstanceRepository.MitigatingCircumstanceLevels.ToList(), "MitigatingCircumstanceLevelId", "Name");
this.ProblemType = new SelectList(sqlProblemTypeRepository.ProblemTypes.ToList(), "ProblemTypeId", "TypeName");
this.ProblemUsers = new MultiSelectList(sqlUserRepository.Users.Where(s => s.UserName != username).ToList(), "UserId", "UserName");
}
}

这是在导航到问题/创建 Controller 方法时生成的:

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

return View("Edit", new ProblemViewModel(new Problem(), sqlStudentRepository,
sqlCommunicationTypeRepository, sqlMitigatingCircumstanceRepository,
sqlProblemTypeRepository, sqlUserRepository, username));
}

这是 ascx View :

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<BournemouthUniversity.WebUI.Models.ProblemViewModel>" %>
        <div class="editor-field">
<%: Html.HiddenFor(model => model.Problem.ProblemId)%>
<%: Html.ValidationMessageFor(model => model.Problem.ProblemId)%>
</div>

<div class="editor-label">
<%: Html.LabelFor(model => model.Problem.StudentId) %>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.Problem.StudentId, Model.Students)%>
<%: Html.ValidationMessageFor(model => model.Problem.StudentId)%>
</div>

<div class="editor-label">
<%: Html.LabelFor(model => model.Problem.CommunicationTypeId)%>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.Problem.CommunicationTypeId, Model.CommunicationType)%>
<%: Html.ValidationMessageFor(model => model.Problem.CommunicationTypeId)%>
</div>

<div class="editor-label">
<%: Html.LabelFor(model => model.Problem.ProblemTypeId)%>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.Problem.ProblemTypeId, Model.ProblemType)%>
<%: Html.ValidationMessageFor(model => model.Problem.ProblemTypeId)%>
</div>

<div class="editor-label">
<%: Html.LabelFor(model => model.Problem.MitigatingCircumstanceLevelId)%>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.Problem.MitigatingCircumstanceLevelId, Model.MitigatingCircumstanceLevel)%>
<%: Html.ValidationMessageFor(model => model.Problem.MitigatingCircumstanceLevelId)%>
</div>

<div class="editor-label">
<%: Html.LabelFor(model => model.Problem.DateTime)%>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Problem.DateTime, new { @class = "datePicker" })%>
<%: Html.ValidationMessageFor(model => model.Problem.DateTime)%>
</div>

<div class="editor-label">
<%: Html.LabelFor(model => model.Problem.Outline)%>
</div>
<div class="editor-field">
<%: Html.TextAreaFor(model => model.Problem.Outline, 6, 70, new { maxlength = 255 })%>
<%: Html.ValidationMessageFor(model => model.Problem.Outline)%>
</div>

<div class="editor-label">
<%: Html.LabelFor(model => model.Problem.AbsentFrom)%>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Problem.AbsentFrom, new { @class = "datePicker" })%>
<%: Html.ValidationMessageFor(model => model.Problem.AbsentFrom)%>
</div>

<div class="editor-label">
<%: Html.LabelFor(model => model.Problem.AbsentUntil)%>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Problem.AbsentUntil, new { @class = "datePicker" })%>
<%: Html.ValidationMessageFor(model => model.Problem.AbsentUntil)%>
</div>

<div class="editor-label">
<%: Html.LabelFor(model => model.Problem.RequestedFollowUp)%>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Problem.RequestedFollowUp, new { @class = "dateTimePicker" })%>
<%: Html.ValidationMessageFor(model => model.Problem.RequestedFollowUp)%>
</div>

<div class="editor-label">
<%: Html.LabelFor(model => model.Problem.Users)%>
</div>
<div class="editor-field">
<%: Html.ListBoxFor(model => model.Problem.Users, Model.ProblemUsers, new { @class = "multiselect" })%>
<%: Html.ValidationMessageFor(model => model.Problem.Users)%>
</div>

<p>
<input type="submit" class="button" value="Save" />
</p>

<% } %>

但是,当我提交表单时,输入了 [HttpPost] Edit Controller 操作,但大多数值都为 null...

[HttpPost]
public ActionResult Edit(Problem problemValues)
{
try
{
MembershipUser myObject = Membership.GetUser();
String UserId = myObject.ProviderUserKey.ToString();

Problem problem = problemValues.ProblemId == 0
? new Problem()
: sqlProblemRepository.Problems(UserId).First(p => p.ProblemId == problemValues.ProblemId);
TryUpdateModel(problem);

if (ModelState.IsValid)
{
sqlProblemRepository.SaveProblem(problem);
TempData["message"] = problem.ProblemId + " has been saved.";

if (Request.IsAjaxRequest())
{
return Json(problem);
}

return RedirectToAction("Details", "Student", new { problem.StudentId });
}
else
return View(problem);
}
catch (Exception ex)
{
if (Request.IsAjaxRequest())
{
return Json(null);
}
else
{
TempData["message"] = "Record Not Found.";
return RedirectToAction("Index");
}
}
}

alt text

任何关于此的想法都将不胜感激,它似乎发生在我有下拉列表的大多数表单上,但我不明白为什么所有值都为空,即使是非下拉字段也是如此。

提前致谢...

乔纳森

最佳答案

我建议您将存储库与模型分开。这样你传递给 View 的就是模型。 View 和 ViewModel 都不需要任何存储库。它的工作方式是 Controller 使用存储库来获取模型并将该模型传递给 View :

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

return View("Edit", viewModel);
}

提交 Action :

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

关于未在 Post 上设置 ASP.NET MVC 表单值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3626362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com