我只是了解asp.net MVC。此Web正在使用WCF Rest服务。基本上,这个Web可以从WCF服务读取和写入数据。但是当我进入编辑表单时,除了下拉列表外,所有表单都填充了模型值。
首先,让我告诉你我的模特:
public class Assignments
{
[Display(Name="Subject")]
public String asgn_subject { get; set; }
[Display(Name = "Assignment Number")]
public String asgn_id { get; set; }
[Display(Name = "Trouble Ticket")]
public String asgn_ticket { get; set; }
[Display(Name = "Status")]
public String asgn_status { get; set; }
[Display(Name = "Start Date")]
public String asgn_start { get; set; }
[Display(Name = "Remark")]
public String asgn_remark { get; set; }
[DataType(DataType.DateTime)]
[Display(Name = "End Date")]
//[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy hh:mm:ss tt}", ApplyFormatInEditMode = true)]
public DateTime? asgn_end { get; set; }
[Display(Name = "Acknowledge Time")]
[DataType(DataType.DateTime)]
//[DisplayFormat(DataFormatString = "{0:yyy-MM-dd hh:mm:ss}", ApplyFormatInEditMode = true)]
public DateTime? asgn_ack_time { get; set; }
}
我的ActionResult脚本:
public ActionResult Edit(string asgn_id)
{
AssignmentServiceClient asc = new AssignmentServiceClient();
AssignmentsViewModels avm = new AssignmentsViewModels();
avm.Assignments = asc.GetAsgn(asgn_id);
return View("Edit", avm);
}
该模型与WCF服务模型相同。 asgn_status
正在返回一个字符串。
在下拉列表字段中,如何根据模型值设置下拉列表?
@Html.DropDownListFor(model => model.Assignments.asgn_status, new List<SelectListItem>
{new SelectListItem {Text="In Progress", Value="True", Selected=true},
new SelectListItem{Text="Completed", Value="False"}}, new { @class = "form-control", style = "width:300px; height:35px;" })
修改
获取方法
public Assignments GetAsgn(string asgn_id)
{
try
{
var webClient = new WebClient();
string url = string.Format(BASE_URL + "GetAsgn/{0}", asgn_id);
var json = webClient.DownloadString(url);
var js = new JavaScriptSerializer();
return js.Deserialize<Assignments>(json);
}
catch
{
return null;
}
}