0

I'm new to both ASP.NET MVC and Nhibernate

I came across this requirement which I have to search a table in display the data in the same page. Users should be able to search by four inputs. Name, ID, Date of birth and city. First three should be keyed in and the city should be selected from a dropdown which is populated from distinct values of the city coulmn.

I have developed upto a level users can search from Name, ID, Date of birth. But i'm having troubles in binding values to a dropdown.

Could someone please help me out in this.

1 Answer 1

1

Ok i found a way, but I'm not sure whether it's the best approach.

a new Model class

public class Countries
{
    public virtual string citizenship { get; set; }
}

in the controller, in my case in both get and post

TDAL tda = new TDAL();

        //////////////////////

        List<SelectListItem> list= new List<SelectListItem>();
        list.Add(new SelectListItem { Text = "All", Value = "0" });
        var cat = tda.getCountries().ToArray();
        for (int i = 0; i < cat.Length; i++)
        {
            list.Add(new SelectListItem
            {
                Text = cat[i].citizenship,
                Value = cat[i].citizenship.ToString(),
                Selected = (cat[i].citizenship == "1")
             });
        }
        ViewData["citizen"] = list;
        ////////////////////////////
        return View();

DAL class

    public IList<Countries> getCountries()
    {
        IList<Ter.Models.Countries> countries;
        IQuery query;
        using (ISession session = OpenSession())
        {
            try
            {
                 query = session
    .CreateQuery("select distinct citizenship AS citizenship from Ters")
    .SetResultTransformer(Transformers.AliasToBean<Countries>());
            }
            catch (Exception c) 
            {
                query = null;
            }

             countries = query.List<Countries>();
        }

        return countries;

    }

and the view

@Html.DropDownList("citizenship", (IEnumerable<SelectListItem>)ViewData["citizen"], new { id = "citizenship" })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.