简体   繁体   中英

Populate combobox in ASP.NET MVC with data from a WCF service

I have created a basic application which works with a WCF service. The application does basic insert update delete operations.

I have defined my IService interface like this:

[ServiceContract]
public interface IService1
{
        [OperationContract]
        IEnumerable<Person> GetPersons();
        [OperationContract]
        IEnumerable<Person> GetPersonsByFNameAndLName(string FName,string LName);
        [OperationContract]
        void InsertPerson(Person personobj);
        [OperationContract]
        void UpdatePerson(Person personobj);
        [OperationContract]
        void DeletePerson(int id);
}

[DataContract]
public class Person
{
        [DataMember]
        [Key]
        [Required]
        public int Id { get; set; }

        [DataMember]
        [Required]
        public string FName { get; set; }

        [DataMember]
        [Required]
        public string MName { get; set; }

        [DataMember]
        [Required]
        public string LName { get; set; }

        [DataMember]
        [Required]
        public DateTime DOB { get; set; }

        [DataMember]
        [Required]
        public string Adddress { get; set; }

        [DataMember]
        [Required]
        public string NIC { get; set; }
}

This is the code of my service1.svc.cs class file (it has the code segments for methods for getting the list of persons and updating the person)

public class Service1 : IService1
{
        public IEnumerable<Person> GetPersons()
        {
            List<Person> personList = new List<Person>();
            PersonContext po = new PersonContext();
            personList = po.Persons.ToList();
            return personList;
        }

        public void UpdatePerson(Person personobj)
        {
            PersonContext po = new PersonContext();
            var c = (from per in po.Persons
                     where per.Id == personobj.Id
                     select per).First();
            c.FName = personobj.FName;
            c.LName = personobj.LName;
            c.MName = personobj.MName;
            po.SaveChanges();
        }
}

and my database context class looks this:

public class PersonContext : DbContext
{
        public PersonContext() : base("PersonCS")
        {
        }

        public DbSet<Person> Persons { get; set; }    
}

I am using PersonCS database connection in web.config file to connect to my database.

I have added this service as a service reference to my ASP.NET MVC project.

This is my code for controller class in my ASP.NET MVC project. The code segment has the methods for view and update.

public class PersonController : Controller
{
        public ActionResult getPersons()
        {
            Service1Client SEObj = new Service1Client();
            List<Person>PeLi =SEObj.GetPersons().ToList();
            ViewBag.List = PeLi;
            return View();
        }

        public ActionResult updatePerson()
        {
            return View();
        }

        [HttpPost]
        public ActionResult updatePerson(Person personobj)
        {
            Service1Client SCOBJ = new Service1Client();
            SCOBJ.UpdatePerson(personobj);
            return View();
        }
} 

Here is my code behind the view:

<form method="post" action="@Url.Action("updatePerson")">

    ID:<input type="text" name="Id" /> 
    <br />
    First Name: <input type="text" name="FName" />
    <br />
    Middle Name: <input type="text" name="MName" />
    <br />
    Last Name: <input type="text" name="LName" />
    <br />
    Date of Birth:<input type="date" id="start" name="DOB" value="2018-07-22" min="1900-01-01" max="2000-12-31" />
    <br />
    NIC:<input type="text" name="NIC" />
    <br />
    Address:<input type="text" name="Adddress" />
    <br />
    <input type="submit" value="Insert" />

</form>

In the view I am trying to manually enter the ID and delete.

What I want to do is populate a combobox with the ids available in the database and select from the ids in the combobox.

Can anyone help me or guide me to achieve that?

I would suggest to pass a List<SelectListItems> to your view. This would reduce traffic from your Controller to your view:

public ActionResult updatePerson()
{
    Service1Client SEObj = new Service1Client();
    List<Person>PeLi =SEObj.GetPersons().ToList();
    ViewBag.List = PeLi.Select(x => new SelectListItems {
        Value = x.Id,
        Text = x.FName + " " + x.LName
    });

    return View();
}

In ViewBag.List is now a List<SelectListItems> of your persons.

in the view you just need to display this list like:

@Html.DropDownList("personId", new SelectList(ViewBag.List, "Value", "Test"))

This will display a dropdown.

When it comes to posting your form to your controller you should ask a new question or look for other answers how to receive values from your form on the controller side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM