getting data from lookup box

Mar 20, 2011 at 6:06 PM
Edited Mar 20, 2011 at 6:31 PM

hi,

I'm using one of your old codebases, The controller code and aspx code is below...........

 

 public class CountryLookupController : LookupController
    {
        //data storage, for demo purposes
        private static readonly IEnumerable<Country> Data = new[] {
            new Country {Id = 1, Name = "Austria"},
            new Country {Id = 2, Name = "Norway"},
            new Country {Id = 3, Name = "Belgium"},
            new Country {Id = 4, Name = "Australia"},
            new Country {Id = 5, Name = "New Zealand"},
            new Country {Id = 6, Name = "Brazil"},
            new Country {Id = 7, Name = "Japan"},
        };

        [HttpPost]
        public ActionResult LookupList(string search)
        {
            ViewData["structure"] = new LookupListInfo
            {
                Key = "Id", // the property from Country that is going to serve as a key
                Columns = new[] { "Name" }, // an array of properties from Country that are going to be shown in the list
                Captions = new[] { "the country name" } // the captions for each ^above columns, to be used in table header
                //if captions aren't specified columns are used instead
            };
            return View(@"Awesome\LookupList", Data.Where(o => o.Name.ToLower().Contains(search.ToLower())));
        }

        //this returns the string that is shown in the disabled textbox near the lookup button
        public ActionResult Get(int id)
        {
            return Content(id == 0 ? "" : Data.Where(o => o.Id == id).Single().Name);
        }
    }

 

 

 public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            var input = new Country
            {
                
            };
            return View(input);
        }

        public String Test(string name, string id)
        {
            if (String.IsNullOrEmpty(id))
            {
                return "Error";
            }
            else if (String.IsNullOrEmpty(name))
            {
                return "Error";
            }
            else
            {

                return "OK";
            }
        }

ASPX CODE

<h2>Index</h2>
     <div>
            Country * :</div>
        <div>
            <%=Html.Lookup("Country", Model.Id, title: "select a country (type some letters from the start like 'us' and hit search/enter)", clearButton:false) %>
        </div>
        <div class="button" id="SFS" style="width: 300px;">
         <%: Ajax.ActionLink("Test", "Test", "Home", new {name=Model.Name, id = Model.Id }, new AjaxOptions { UpdateTargetId = "SFS", InsertionMode = InsertionMode.Replace, HttpMethod = "Post" }, new { style = "color:#00FF00" })%> 
         </div>

I'm able to use lookup but in ajax.actionlink I get Model.Name=null and Model.Id=0.
Preferably I would like to use your confirmbox before calling the Test method.
Sincerely appreciate if you could help...
If you need the solution files, please let me know your email id....
Thanks 
Arnab
Mar 20, 2011 at 6:53 PM

what are you trying to do, and why aren't you using the latest version ?

Mar 21, 2011 at 3:00 AM

I'm updating a feature in a project where I used awesome some time back.

What I'm trying to do: Once the user gets a value from lookup, sending that value to a method in a controller using ajax, but I'm unable to get the value of lookup in ajax.actionlink...

Regards

Arnab

Mar 21, 2011 at 6:19 AM

I even changed the ajax actionlink to a simple submit but still I get Model.Name=null at controller.

ASPX

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication5.Models.Country>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2> <%= ViewData["InviteSent"] %></h2>
     <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>
     <div>
            Country * :</div>
        <div>
            <%=Html.Lookup("Country", Model.Id, title: "select a country (type some letters from the start like 'us' and hit search/enter)", clearButton:false) %>
        </div>
       <%-- <div class="button" id="SFS" style="width: 300px;">
         <%: Ajax.ActionLink("Test", "Test", "Home", new {name=Model.Name, id = Model.Id }, new AjaxOptions { UpdateTargetId = "SFS", InsertionMode = InsertionMode.Replace, HttpMethod = "Post" }, new { style = "color:#00FF00" })%> 
         </div> --%> 
         <p>
                <input type="submit" value="Send Request" />
            </p>
             <% } %>
</asp:Content>

Home Controller

public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            var input = new Country
            {
                
            };
            return View(input);
        }
         [HttpPost]
        public ActionResult Index(Country model)
        {
           if (String.IsNullOrEmpty(model.Name)) //model.Name Is null
            {
                ViewData["InviteSent"] = "<p style=\"color:blue\">Invitation not sent successfully.</p>";
            }
            else
            {

                ViewData["InviteSent"] = "<p style=\"color:blue\">Invitation sent successfully.</p>";
            }
            return View();
        }

    }



Mar 21, 2011 at 7:56 AM

I don't know how the ajax action link works but I thinks it's for microsoft ajax not jquery,

if all you need is to make a link which will include the value of the lookup in it's parameters, write a js function that will do that,

use firebug to see from where to get the value for that parameter, and after bind the js function to the link or just put <a href='javascript:yourfunc()'

(use w3cschools, stackoverflow, google, if you don't know js or jquery  )

Mar 21, 2011 at 9:59 AM

Thanks for the reply....

The error was completely because of my mistake, I was trying to use country model class where I should  have been using Person model( simpledemo).

Now I'm able to get the country Id value back as it is there in Person. 

But I need to get the countryName as well.

One way would be to do a second query to get the country name for that countryId.

Is there a better way?

Regards

Arnab

Mar 21, 2011 at 10:09 AM

you have the id now get the name, that's the best way

Mar 21, 2011 at 10:36 AM

Thanks.