Click here to Skip to main content
15,887,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I collecting a value from database. and i want to send that value to view.and their i want to check that value.

What I have tried:

In Action method in controller
public ActionResult BookSet(long? ClassId)
{
List<bkselect> Bk = new List<bkselect>();

Bk= (from a in db.Student_Book_Allow_Quantity
where a.Class_Id==ClassId
select new BKselect()
{
Class_Id=a.Class_Id,
Student_Book_Allow_Quantity_Id=a.Student_Book_Allow_Quantity_Id
}).OrderBy(a => a.Class_Id).ToList();
ViewBag.BKselect = Bk.ToList();
return PartialView();
}
in view part
$("#FieldListTable").append('
<input class="addNewButton" style="float: left; margin-left: 0px;" type="button" value="Add" id="btnAdd" /><input class="addNewButton" style="float: left; margin-left: 0px;" type="button" id="btnRemove" value="Remove" />
BOOkS LISt
@Html.ListBox("LstSignInAuthority", Enumerable.Empty<SelectListItem>(), new { @id = "LstSignInAuthority", style = "width:200px;height:130px;" })
');
fuction part
function Addbook() {
var bkLst = $("#LstSignInAuthority");
if (bkLst !== "") {
$(document).ready(function () {


var newValue = $('input[name=Book_Name]').val();
var TotalItems = document.getElementById("LstSignInAuthority").options.length;

if (TotalItems < 5) {
$('#LstSignInAuthority').append('<option value="' + newValue + '">' + newValue + '</option>');
$("#Book_Name").val("");
$("#Title").val("");
$("#Book_Category_Name").val("");

}

});



}

}
Posted
Updated 11-Mar-16 1:00am
Comments
[no name] 11-Mar-16 4:52am    
DO you want to return int value or list value to view from your controller. Because in your code you are returning List but in question header it says int. Please confirm..
Hussain Javed 11-Mar-16 5:10am    
i want to return value of total books allocated to particular class.that value i want to return to view.and i want to compare that value with total items in listbox control count value.in Add function.

The best option is to make an ajax call to that action method,
 
Share this answer
 
Comments
Hussain Javed 11-Mar-16 4:05am    
i dont know to make ajax call to action method,can you please help me
how to call controller action method and pick up that int value and use that value in add function of view
In following code, it sends an AJAX request to server and it fetches all books. Next it checks total book list length with dropdown list length. Find below steps to get your requirement:

JavaScript:
JavaScript
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script> 
    var jsonObj = { ClassId : 234 };
    alert(JSON.stringify(jsonObj));
 
    $.ajax({
        type: "POST",
        url: "/Home/BookSet", // Home is contoller, Bookset is action
        data: JSON.stringify(jsonObj),
        contentType: "application/json",
        datatype: "json",
        success: function (bookList) {
            if(bookList.length == $("#LstSignInAuthority option").length)
			{
				// Implement your logic
			}else
			{
				// Implement your logic
			}
        }
    });
</script>

Controller action:
C#
[HttpPost]
public ActionResult BookSet(long? ClassId)
{
	List<bkselect> Bk = new List<bkselect>();

	Bk= (from a in db.Student_Book_Allow_Quantity
		where a.Class_Id==ClassId
		select new BKselect()
		{
			Class_Id=a.Class_Id,
			Student_Book_Allow_Quantity_Id=a.Student_Book_Allow_Quantity_Id
		}).OrderBy(a => a.Class_Id).ToList();
 
	return Json(Bk);
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900