0

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;
            }
        }
Icon
  • 103
  • 9
  • Its the value of `asgn_status` that determines what is selected. Set the value of that property in the GET method to match one of the option values (and remove `Selected = true` since that is ignored). But why is the property a `string` when your creating options for a `bool`? –  Aug 22 '17 at 11:52
  • @StephenMuecke: The WCF Services model is String. Didn't i need to make my model equal to WCF services model ? – Icon Aug 22 '17 at 12:35
  • 1
    I suspect the real issue is that your option values are `"True"` and `"False"` (which would be appropriated for a `bool` property, but a property named `Status` does not sound like a `bool`). I'm guessing that the value of `asgn_status` should be `"In Progress"` or `"Completed"` - in which case that is what you need to set the `Value` property of `SelectListItem` to (and if the value of `asgn_status` is `"Completed", then the 2nd option will be selected) –  Aug 22 '17 at 12:40
  • @StephenMuecke: Is there a way to set dropdown list based on model value ? some thing like : `if(asgn_status.Equals("Completed")){ //Here i set dropdownlist value }` . Where should i put the logic ? in the controller ? – Icon Aug 22 '17 at 12:57
  • 1
    Did you not understand the previous comment? If its `{ new SelectListItem {Text="In Progress", Value="In Progress" }, new SelectListItem{Text="Completed", Value="Completed"}` and the value of `asgn_status` is `"In Progress"` then the first option will be selected. If the value of `asgn_status` is `"Completed"` then the 2nd option will be. –  Aug 22 '17 at 13:00
  • @StephenMuecke: Ah i got it, thanks. You can answer my question below, then i will mark your answer. – Icon Aug 22 '17 at 13:03

0 Answers0