How to use the array of values in the WHERE clause of my query - asp.net-mvc-3

I am using asp.net mvc3. I want to pass an array from view to the controller using parameterMap as shown below. In my view:
parameterMap:
function (data, options) {
if (options === "read") {
sessionStorage.setItem("value","array");
val = sessionStorage.getItem("value"); // contains array
return { model: JSON.stringify(val) }; //passing array to controller
}
}
In controller:
public ActionResult SearchDetails( string model)
{
var query = (from ......).where();//want to compare array values in controller
}
but I am not able to retrieve those values in the controller. How can I retrieve these array of values in my controller without using looping statements?
My array contains only integer values (ids), while debugging the when I put cursor at the parameter of action method the values are coming in ""[{\"id\":1},{\"id\":2}]"" format. How can I use this array of values in my WHERE clause of my query?

if you are bound and determined to stick with the string input, I would think a call to Split would work. However, it would be much easier if you changed from bringing it in as a string to just bringing in the model. Have you tried something like:
public ActionResult SearchDetails( model model)
{
var query = (from ......).where();//want to compare array values in controller
}
are you trying to do something equivalent to a SQL IN? If that is the case then use .Contains in your query. For example:
public ActionResult SearchDetails(model model)
{
var query = from d in context.data
where model.Contains(d.id)
select d;
}

Related

Using MVC Core Action Method, I want to pass JsonResult to the view. Data is retrieved from Models. What is the best way to do the same?

Method 1:
Here data retrieved from database is converted to Json in Repository class and is passed to Controller
Repository class method to retrieve data:
public JsonResult GetEmployee(int Id)
{
Employee emp = this._employeeList.FirstOrDefault(e => e.Id == Id);
return new JsonResult(emp);
}
Action Method in Controller:
public ViewResult Details()
{
JsonResult model = _employeeRepository.GetEmployee(1);
return View(model);
}
Method 2:
Here data retrieved from database is converted to Json in Controller class at respective action Method
Repository class method to retrieve data:
public Employee GetEmployee(int Id)
{
return this._employeeList.FirstOrDefault(e => e.Id == Id);
}
Action Method in Controller:
public ViewResult Details()
{
Employee model = _employeeRepository.GetEmployee(1);
return View(Json(model));
}
Which is the best method in terms of performance and other parameters?
P.s: I want to use the json data passed in View for Ajax scripts to display computed data
I would only render JSON into a view if I needed it to be part of a html response, in which case I'd serialise it as a string first, and pass it through as a property of my model.
If I also want to pass the same data via ajax, I would write a second function in my controller e.g. Details_ajax, which would return JsonResult and use return Json(data) to serialise the data.
In general, avoiding serialisation and deserialisation is most efficient where possible.

Passing several query parameters to a GET endpoint in .netcore web api

I have an application where there will be several parameters passed to my endpoint for searching, these parameters are not defined because they are dynamically generated so i cannot map it to a specific model. What would be the best way to map any query parameters into my GET endpoint?
[HttpGet]
public CustomResponse GetResults({something here that will map the parameters?})
{
//perhaps a dictionary? a collection of some sort?
}
Then I need to get all those keys and values and search the database for anything containing that and as i said it could be anything.
So I could pass something like?
/api/Merchandise/GetResults?sku=30021&cupsize=medium&color=red&location=south& {and all the dynamic fields which could be anything}
HttpRequest object has Query property that is an IQueryCollection and holds all passed query parameters.
In other words, in your action method you may do:
[HttpGet]
public CustomResponse GetResults()
{
var queryParams = HttpContext.Request.Query;
// directly get by name
var value1 = queryParams["parameter_name"];
// or queryParams.TryGetValue()
foreach (var parameter in queryParams)
{
string name = parameter.Key;
object value = parameter.Value;
}
}
You could map it to JObject, which is like a Dictionary.
Don't forget:
using Newtonsoft.Json;

How to show custom query result to view mvc3 Razor

I have Custom query Result as
public ActionResult Index()
{
var Query = (from E in db.SITE_MASTER.AsEnumerable()
where E.IS_PAGE == true
select new
{
E.POST_TITLE,
E.POST_TEXT
}).ToList();
return View(Query);
}
Now,
How can I make view for this result or How can I create View for this Result Query.
Thanks...
You can pass an anonymous type as a model, but please don't. It's ugly and will lead to maintenance problems.
As an alternative either use the ViewBag or create concrete type and pass that.

Returning a list to view and manipulating it in MVC3, Database first approach

I have a function that triggers a stored procedure that runs a
select * from tbl_admin where id=".." and username=".."
The returned output of the select statement is stored in a complex return type object spa_user_Result and stored onto a list.
I want to pass this list to the view and use it to display data, How do I do that??
code is smthing like this:
public ViewResult Index()
{
<spa_users_Result> result = new <spa_users_Result>();
System.Data.Objects.ObjectResult<spa_users_Result> r;
r = db.adminUser("s","superuser");
result = (<spa_users_Result>)r.ToList();
return View();
}
You need to pass your object(s) to the View method
public ActionResult YourAction()
{
var result = db.adminUser("s","superuser").ToList();
return View(result );
}
Assuming your db.adminUser("s","superuser") method returns a Collection of spa_users_Result object.
Make your View strongly typed to List of spa_users_Result object
#model IEnumerable<spa_users_Result>
#foreach(var item in Model)
{
<p>#item.Name</p>
}
Assuming your spa_users_Result class has a Property called Name
I'm not entirely sure what level of detail you're looking for with this question, but passing the list to the view is as simple as:
return View(result);
EDIT: Also don't you want result to be a list?
List<spa_users_Result> result = new List<spa_users_Result>();

MVC ddl value after postback

I have a ddl which is populated with hours of day 01-23. This is on a form which is used to book an item of equipment. The hour is populated to a db field. The issue is this, when the booking form is opened to alter the time the ddl shows the hour that was booked, when changed though and the form is submitted the value passed on post is the initial value from db not the new selected hour.
this is the basic pieces of code. any idea why the newly selected ddl value is not passed to the model??
View
<%= Html.DropDownList("ddl_Hour", Model.ddlHour,
new { #class = "DropDown", style = "width: 40px” })%>
Model
private string _ddlHourSelectedValue = "0";
public SelectList ddlHour
{
get
{
return (new System.Web.Mvc.SelectList(_ddlHour, "intValue", "Text", Convert.ToInt32(_ddlHourSelectedValue)));
}
}
public string ddlHourSelectedValue
{
get
{
return _ddlHourSelectedValue;
}
set
{
_ddlHourSelectedValue = value;
}
}
param[6] = new SqlParameter("#Timeslot", ddlHourSelectedValue);
The field in your view is called "ddl_Hour" However is there a variable in your Model with the same name? Otherwise the MVC framework will not automatically populate the value in the model.
Two ways you could go about this.
1
In your controller methods that accepts a post, you can add the parameter: FormCollection fc to the method. This key value pair collection will allow you to fetch results from fields in the post data like so:
string selectedValue = fc["ddl_Hour"];
2
Or you can modify your model to include a variable with the same name as the drop down list so that it is automatically populated for you.
public string ddl_Hour { get; set; }
You should then be able to access the result of the drop down list selection on post from that variable.

Resources