I have the following controller to take four parameter and one to get an array of string.
public Response Get(string a, double b, double c, string[] d)
{
do something
}
As u see the fourth(d) parameter is an array of strings. The first 3 parameter get the value but d shows null.
I am using fiddler to debug with following url
04/api/Controller?a=Hello&b=-37.8031231&c=144.9836514&d=Italian&d=bars
What m i doing wrong? is it the url?
Without seeing your View, I can abstract that you named 2 inputs as name='d' without indexing
example:
You can create an array by naming your id inputs with array indexes.
#using (Html.BeginForm())
{
<input type="text" name="d[1]">
<input type="text" name="d[2]">
}
If you index your inputs, the framework will use them as a List
Note:
you bring up URL. You cannot pass an array or any complex object without a Post.
You can post array data from fiddler, the URL you have provided is correct. But make sure you have the correct content type in request header.
you don't have to create view to test your controller action method.
Client request
Server respone
P.S. Above provided is for controller action method, I have noticed you are using api controller after posting the answer. I will edit the answer once done for api controller.
The Above works for APIController as well, If you are posting from fiddler
Related
I have an action method with the following signature,
ActionResult Search(string searchQuery)
This gets called from a partial view on button submit from a form. Problem is, please look at the 2 patterns below. When I submit my search key from my page it uses the following url (suppose search key is tool)
Search/?searchQuery=tool
But then if I click on a tool then,
Search/tool?searchQuery=garden
Now my method is reading tool in the parameter instead of garden (which is expected of course). I presume this is to do with incorrect presentation of items from both the context of the item itself and that of search.
Is there a nice way of resolving this issue? I want to read the query string term and search for it from the main search context i.e. Search/?searchQuery=<term> no matter where I am.
To get the QueryString, in your controller you should write something like this:
var mystring =Request.QueryString["searchQuery"];
This will get the query string no matter where is placed in your url.
Rename the input to
ActionResult Search(string searchQuery)
The model binder will then deserialize the query string param to that input value. It will work for both route params and query string params.
I have an MVC WebApi app. I'm trying to do something basic - pass a string via JSON in the body. My client submits a small number of key/value parameters, and when the MVC router gets them, it begins to interpret the content of the strings.
An example JSON body is
{ "myKey":"red,yellow,brown,orange","foo":"bar" }
My MVC controller method is
public Dictionary<string, string> PostMyAction([FromBody] str1, [FromBody] str2) { }
I would expect str1 == "red,yellow,brown,orange" but instead I get "Can't bind multiple parameters ('str1') to the request's content."
Why is it parsing the first string as a list of parameters?
Shouldn't your Controller method be an ActionResult?
You do can receive more than one parameter on your actions, but for a clear code, I recomend use one ViewModel that contains properties that will represent your View, and Binded on submit.
I want to pass insured id to controller while getting insured details which looks like:
// GET: /Insured/Details/123456789
But I don't want to pass this id number 123456789 in query string for security reasons.
Can somebody suggest me the best way. I am new to MVC.
Thanks
Try submitting the value as a hidden parameter via POST. You'll need to use a form to do POST submits though. You can't use links.
<form method="POST" action="/Insured/Details">
<input type="hidden" name="insuredid" value="123456789"/>
<input type="submit"/>
</form>
You would then get the value via request parameter. If using Java servlets, it would look something like this:
String myInsuredId = request.getParameter("insuredid");
I imagine it would look similar to other platforms as well.
NOTE: Although passing values as hidden parameters hides the data from view, this does not prevent people from sniffing the submitted data or checking the HTML source of your page. If you really want to make this secure, you'll need to use some form of encryption/security. Try looking into HTTPS posts.
You need to sanitize your input in the controller, for example if the customer is logged in you could check that the ID passed to the controller truly belongs to the customer.
Anything that is passable in request (whether it's POST or GET) is spoofable.
You should also look into serving the page over HTTPS.
E.g. (asp.net MVC / C#)
public ActionResult Details(string id)
{
if (Check(id) == false)
{
// Handle invalid input
throw new HttpException(404, "HTTP/1.1 404 Not Found");
}
// create the model
...
}
I am writing my first ASP.Net webpage and using MVC.
I have a string that I am building in a partial view with a grid control (DevExpress MVCxGridView). In my partial view I am using a HTML.Hidden helper as shown below.
' Create a hidden variable to pass back a comma-delimited string
Response.Write(Html.Hidden( "exclusionList", Model.ExclusionList))
The value of of this hidden element is assigned in client side javaScript:
exclusionListElement = document.getElementById("exclusionList");
// ...
exclusionString = getExclusionString();
exclusionListElement.value = exclusionString;
This seems to work without problem.
In my controller action method:
<AcceptVerbs( HttpVerbs.Post )> _
Public Function MyPartialCallback(updatedItemList As myModel) As ActionResult
Dim myData As myModel = GetMyModel()
Return PartialView( "MyPartial", myModel.myList )
End Function
The updatedItemList parameter is always nothing and exclusion list exists no where in the Request.Forms.
My questions are:
What is the correct way to use Html.Hidden so that I can access data in a MVC Controller Action method.
Is adding "cargo" variables to Request.Form the best and only way to send data back to a server side MVC Controller Action method? It just seems like twine and duct-tape approach. Is there a more structured approach?
If you need to get the exclusionList variable back, you just need to add a property to your view model that matches that name exactly. Make sure it is of the correct type (string it looks like in this case) and then it should auto populate that property in the view model for you.
And yes, there is no need for the Response.Write call. Instead just use the Html.HiddenFor(...) helper in your view.
Look at the generated HTML. Note down the name attribute of the hidden field. Use this name as action parameter name:
Public Function MyPartialCallback(exclusionList As string)
I want to call an action with something similar to this uri:
http://server/controller/action/?columns=firstname&columns=lastname&columns=age
and use it like this:
public ActionResult Action(string[] columns)
{
}
how do I do it?
Google is my friend ;)
http://server/controller/action/?columns[]=firstname&columns[]=lastname&columns[]=age
Edit:
Actually you just write as I did in my original question. The reason to why I didn't get it working in the first place is that I used "column" in the query string and "columns" in as action parameter.
I don't know if it's the difference between get and post parameters, but your original post works perfectly good with post parameters. In fact, when using []'s in post parameters the array becomes null in the action parameter. I found this out when jQuery 1.4 started adding []'s in json arrays when posting. See: http://www.dovetailsoftware.com/blogs/kmiller/archive/2010/02/24/jquery-1-4-breaks-asp-net-mvc-actions-with-array-parameters