[RESOLVED]How to get selected value using Jquery

 Hi

In MVC i used entity framework to insert,edit,delete operations .I used database creation first.

It Saves data with out writing single code.

I have Type Code,Type name —-Type code should be saved in database
but the display should be Type Name.I am getting all the values populated in dropdown list using Jquery.

but I want to save Type code in database because typecode is int in database

I have got code in controller

public class ActivitiesController : Controller
{
private TERPV4Entities db = new TERPV4Entities();

public ActionResult Index()
{

return View(db.ACTIVITIES.ToList());
}

public ActionResult Create()
{

ViewData["CurrentDatetime"] = System.DateTime.Now;

List<SelectListItem> li = new List<SelectListItem>();
li.Add(new SelectListItem { Text = "Select", Value = "0" });
li.Add(new SelectListItem { Text = "Task", Value = "1" });
li.Add(new SelectListItem { Text = "Phone call", Value = "2" });
li.Add(new SelectListItem { Text = "Fax", Value = "3" });
li.Add(new SelectListItem { Text = "Email", Value = "4" });
li.Add(new SelectListItem { Text = "Letter", Value = "5" });
li.Add(new SelectListItem { Text = "Campaign Response", Value = "6" });
li.Add(new SelectListItem { Text = "Campaign Activity", Value = "7" });
li.Add(new SelectListItem { Text = "Social Activity", Value = "8" });
ViewData["ListActivities"] = li;

return View();

}

}
I want to save "Value" in the database from ViewData["ListActivities"]

In cshtml file

@Html.Label("Type")

@if (ViewData.ContainsKey("ListActivities"))
{
@Html.DropDownList("ListActivities", ViewData["ListActivities"] as List<SelectListItem>, new { style = "width:250px"})
}

 
<input type="submit" value="Create" />

SelectListItem class contains

public class SelectListItem
{

public SelectListItem();

public bool Selected { get; set; }

public string Text { get; set; }

public string Value { get; set; }
}

I don’t see the insert query .How to find the insert query
Please help me
Thanks
Chandran

it’s pretty easy

$(selector).val();

so in your case it would be something like

$(function() {

var startValue = $('#ListActivities').val();

});

If you need to get the selected value through jQuery, you should be able to simply use the .val() function when targeting your DropDownList element as seen below :

// Get your selected item via jQuery
var selected = $("#ListActivities").val();

This could be useful if you were posting the values or using AJAX to trigger some event with the value. However, if you were just posting your <form> via a traditional submit button, you should be able to access the value within the HttpPost action that
corresponds to your View :

[HttpPost]
public ActionResult Create()
{
      // Access your selected value here
      var selected = Request["ListActivities"];
}

Leave a Reply