我一直在使用Telerik Kendo DropDown的ASP MVC(C#)razor Entity Framework项目Visual Studio 2012中工作:
@(Html.Kendo().DropDownListFor(run => run.Repair)
.Name("Repair")
.OptionLabel(RunStrings.Select)
.DataTextField("Text")
.DataValueField("Value")
.DataSource(dataSource => dataSource
.Read(read => read.Action("GetRepairCodes", "Run")
)
)
.Value(myModel.Repair.ToString())
.HtmlAttributes(new { style = "width:50% !important;" })
.Events( e => e.Change("RepairDropDown_OnChange")))
DropDown将其值发送到对象outcomeModel中的Controller。 Controller的代码是:
public ActionResult OutcomeEdit(OutcomeModel outcomeModel, string previous, string next, string finish)
{
Run run = Session["Run"] as Run;
if (run == null) return RedirectToAction("Index", "Home", new { area = "" });
if (ModelState.IsValid)
{
outcomeModel.CopyToRun(run);
Repository myRepo = new Repository();
myRepo.UpdateOutcome(run);
// now update the Organ failure column
string organFailureString = string.Empty;
if (run.Discontinuation == Discontinuation.DiedOrganFailure && ModelState["OrganFailureType"] != null)
{
organFailureString = outcomeModel.DecodeOrganFailureFromString(ModelState["OrganFailureType"].Value.AttemptedValue).ToString();
myRepo.UpdateOrganFailureValue(run.RunId, organFailureString);
}
else
{
run.OrganFailure = null;
myRepo.UpdateOutcome(run);
}
/***
* This will redirect user to a selected tab
*/
string jumptToTab = Request.Form["JumpToTab"];
if (jumptToTab != "-1")
{
TabIndex gotoindex = (TabIndex)Convert.ToInt32(jumptToTab);
TempData["TabIndex"] = (int)gotoindex;
return RedirectToAction(GiveLinkToPage(gotoindex));
}
// continue normally
// Find which command was selected (non-null parameter)
var selection = (new[] { previous, next, finish })
.Select((item, index) => new { ItemName = item, Position = index })
.First(x => x.ItemName != null);
TabIndex currentTab = TabIndex.SeverityScores;
LinkDirection command = LinkDirection.Finish;
if (selection.Position == 0) { command = LinkDirection.Previous; }
if (selection.Position == 1) { command = LinkDirection.Next; }
if (selection.Position == 2) { command = LinkDirection.Finish; }
TempData.Keep("TabIndex");
TempData["TabIndex"] = this.GiveFutureTabIndex(command, currentTab);
string link = this.GiveFutureLink(command, currentTab);
return RedirectToAction(link);
}
return View(outcomeModel);
}
但是,在outcomeModel中,在Repair属性中,在调试器中我总是看到DropDown的前一个值,而不是选择的值。我找不到原因。所以,我决定在DropDown的JavaScript OnChange事件中强制设置DropDown的值:
<script>
function RepairDropDown_OnChange(e) {
console.log('RepairDropDown_OnChange');
console.log('Model ' + e.model);
console.log('Container ' + e.container.find('#Repair').length);
e.model.set("Repair", e.container.find('#Repair').val());
}
</script>
但是,在浏览器的控制台中,我看到e.model未定义。我该如何解决这个错误?提前感谢您的帮助。
更新 OutcomeModel是:
public class OutcomeModel : RunModelBase
{
#region Construction
public OutcomeModel() { }
public OutcomeModel(Run run)
{
if (Mapper.FindTypeMapFor<DateTimeOffset, DateTime>() == null)
{
Mapper.CreateMap<DateTimeOffset, DateTime>().ConvertUsing<DateTimeOffsetConverter>();
}
if (Mapper.FindTypeMapFor<Run, OutcomeModel>() == null)
{
Mapper.CreateMap<Run, OutcomeModel>()
.ForMember(dest => dest.UniqueId, opt => opt.MapFrom(src => src.Patient.UniqueId))
.ForMember(dest => dest.Birthdate, opt => opt.MapFrom(src => src.Patient.Birthdate))
.ForMember(dest => dest.Sex, opt => opt.MapFrom(src => src.Patient.Sex))
.ForMember(dest => dest.Race, opt => opt.MapFrom(src => src.Patient.Race))
.IgnoreAllNonExisting();
Mapper.AssertConfigurationIsValid();
}
Mapper.Map(run, this);
}
#endregion Construction
#region Properties
public string OrganFailureType { get; set; }
[Display(Name = "Discontinuation", Description = "DiscontinuationDescription", ResourceType = typeof(RunStrings))]
[UIHint("Enum")]
public Discontinuation? Discontinuation { get; set; }
[Display(Name = "OrganFailure", Description = "OrganFailureDescription", ResourceType = typeof(RunStrings))]
public OrganFailure OrganFailure { get; set; }
[Display(Name = "DischargedAlive", Description = "DischargedAliveDescription", ResourceType = typeof(RunStrings))]
public bool? DischargedAlive { get; set; }
[Display(Name = "DischargeLocation", Description = "DischargeLocationDescription", ResourceType = typeof(RunStrings))]
[UIHint("Enum")]
public DischargeLocation? DischargeLocation { get; set; }
[Display(Name = "ExtubationDate", Description = "ExtubationDateDescription", ResourceType = typeof(RunStrings))]
[DisplayFormat(DataFormatString = "{0:f}")]
[CompareValues("IntubationDate", CompareValues.GreaterThanOrEqualTo, ErrorMessageResourceName = "ExtubationDateError", ErrorMessageResourceType = typeof(RunStrings))]
[CompareValues("AdmitDate", CompareValues.GreaterThanOrEqualTo, ErrorMessageResourceName = "ExtubationDateError", ErrorMessageResourceType = typeof(RunStrings))]
public DateTime? ExtubationDate { get; set; }
[Display(Name = "DischargeDate", Description = "DischargeDateDescription", ResourceType = typeof(RunStrings))]
[DisplayFormat(DataFormatString = "{0:f}")]
[CompareValues("IntubationDate", CompareValues.GreaterThanOrEqualTo, ErrorMessageResourceName = "DischargeDateError", ErrorMessageResourceType = typeof(RunStrings))]
[CompareValues("AdmitDate", CompareValues.GreaterThanOrEqualTo, ErrorMessageResourceName = "DischargeDateError", ErrorMessageResourceType = typeof(RunStrings))]
public DateTime? DischargeDate { get; set; }
[Display(Name = "DeathDate", Description = "DeathDateDescription", ResourceType = typeof(RunStrings))]
[DisplayFormat(DataFormatString = "{0:f}")]
[CompareValues("IntubationDate", CompareValues.GreaterThanOrEqualTo, ErrorMessageResourceName = "DeathDateError", ErrorMessageResourceType = typeof(RunStrings))]
[CompareValues("AdmitDate", CompareValues.GreaterThanOrEqualTo, ErrorMessageResourceName = "DeathDateError", ErrorMessageResourceType = typeof(RunStrings))]
public DateTime? DeathDate { get; set; }
[Display(Name = "Repair", Description = "RepairDescription", ResourceType = typeof(RunStrings))]
public short? Repair { get; set; }
// Following properties are in model for validation, but are not copied/persisted
public DateTime? AdmitDate { get; set; }
public DateTime? IntubationDate { get; set; }
#endregion Properties
UPDATE2 myModel是:
ELSORegistry.DataAccess.Run myModel = Session["run"] as ELSORegistry.DataAccess.Run;