Not able to get listboxfor selected item

Posted by Mokkarasu under ASP.NET MVC on 6/11/2014 | Points: 10 | Views : 3217 | Status : [Member] | Replies : 1
Hi friends,

Am new to mvc.In my application am using listboxfor, In that i can not get selectedlistitem but i can get selected value but exactly i need selectedlistitem. can any one give me the solution.. thanks in advance.

My view is:

@model MvcApplication2.Models.samplemodel
@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Sample</title>
</head>
<body>
<div>
@using (Html.BeginForm("Sample", "Home", FormMethod.Post))
{
@Html.ListBoxFor(m=>m.liget,new MultiSelectList(Model.li,"Value","Text"),new {Multiple = "multiple"})
<input type="submit" name="submit" value="Submit" />
}
</div>
</body>
</html>


my model is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Linq;
using System.Web.Mvc;
namespace MvcApplication2.Models
{
public class samplemodel
{
public List<SelectListItem> li
{
get;
set;
}
public List<SelectListItem> liget
{
get;
set;
}

}
}

My controller is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;

namespace MvcApplication2.Controllers
{
public class HomeController : Controller
{


public ActionResult Sample()
{

samplemodel s = new samplemodel();
s.li = new List<SelectListItem>();

s.li.Add(new SelectListItem { Text = "one", Value = "1" });
s.li.Add(new SelectListItem { Text = "two", Value = "2" });
return View(s);
}
[HttpPost]
public ActionResult Sample(samplemodel s)
{
return View();
}
}
}





Responses

Posted by: Goud.Kv on: 6/11/2014 [Member] [MVP] Gold | Points: 25

Up
0
Down
Hi,
Try replacing the below line in your code.
@Html.ListBoxFor(m=>m.liget,new SelectListItem(Model.li,"Value","Text"),new {Multiple = "multiple"})


and also,
@Html.ListBoxFor(m=>m.liget,new SelectListItem(Model.li,"Text","Value"),new {Multiple = "multiple"})


Thanks & Regards,
Krishna

Mokkarasu, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response