i just learn about asp.net MVC. This web, is consume WCF Rest Services. Basically, this web is can Read and Write data from WCF Services. But when i go into edit form, all form is filled with model value except for dropdownlist.
First, let me show you my model:
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; }
}
My ActionResult script:
public ActionResult Edit(string asgn_id)
{
AssignmentServiceClient asc = new AssignmentServiceClient();
AssignmentsViewModels avm = new AssignmentsViewModels();
avm.Assignments = asc.GetAsgn(asgn_id);
return View("Edit", avm);
}
The model is same as the WCF Services model. The asgn_status
is returning a string.
In the dropdownlist field, how to set the dropdownlist based on the model value ?
@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;" })
EDIT
Get method
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;
}
}