MVC ERROR Illegal characters in path. - asp.net-mvc

can someone tell me if im on the right track? Im trying to display my query but i get an error. I have two textbox with the same parameter and that parameter is declared as an IEnumerable.
[HttpPost]
public ActionResult Orders1(IEnumerable<int> order)
{
using (CostcoEntities1 context = new CostcoEntities1())
{
var query = string.Empty;
foreach (var orderID in order)
{
query = (from a in context.CM_Checkout_Details
where a.CheckoutDetails_ID == orderID
select a).ToString();
}
return View(query);
}
}
this is what my controller looks like..
I am trying to read the two numbers(Id) in the text box and diplay data based on those id.
#using (Html.BeginForm("Orders1", "Track", FormMethod.Post))
{
#Html.TextBox("order")<br />
#Html.TextBox("order")
<input type="submit" value="Submit" />
}

First thing, change the names of the textboxes so that they are not the same:
#using (Html.BeginForm("Orders1", "Track", FormMethod.Post))
{
#Html.TextBox("order1")<br />
#Html.TextBox("order2")
<input type="submit" value="Submit" />
}
Next, change the signature of your action method:
[HttpPost]
public ActionResult Orders1(string order1, string order2)
MVC's model binding will try to match order1 and order2 to stuff in Request.Form, for example, which should pick up the textbox values.

Related

How to pass text value to action parameter?

i need to pass input value to action method in controller
i have text and button , so I need to click on the button and take the value of input to parameter and re back to the same view but in post status
i tried many things but not works
i tried to create post form and the type of button is submit but does not work
this is my view code
<form method="post">
<input type="text" id="Comments" , name="ID" value="" />
<button type="submit">Go</button>
</form>
this is my controller code
public ActionResult Index()
{
Entities db = new Entities();
var Query = from acc in db.tbl_Accounts
select acc ;
var accList= Query.ToList<tbl_Accounts>();
return View(accList);
}
[HttpPost]
public ActionResult Index(int ID)
{
Entities db = new Entities();
var Query = from Acc in db.tbl_Accounts
where Acc.id== ID
select Acc ;
var accList= Query.ToList<tbl_Accounts>();
return View(accList);
}
In your view model use #Html.BeginForm redirect to action method in controller.
In View
#using (Html.BeginForm("youractionname", "yourcontrollername",FormMethod.Post, new { #class = "form-horizontal"}))
{
<input type="text" id="Comments" name="ID" />
<button type="submit">Go</button>
}
In Controller
public ActionResult Index()
{
Entities db = new Entities();
var Query = from acc in db.tbl_Accounts
select acc ;
var accList= Query.ToList<tbl_Accounts>();
return View(accList);
}
[HttpPost]
public ActionResult Index(int ID)
{
Entities db = new Entities();
var Query = from Acc in db.tbl_Accounts
where Acc.id== ID
select Acc ;
var accList= Query.ToList<tbl_Accounts>();
return View(accList);
}
You have taken an integer datatype in the action method so that you enter the digit value in the textbox
Note: Please verify your action and controller name In view Model
There is a few typo mistakes in your code, if you correct it as following then it will work as expected:
<form method="post">
<input type="text" id="Comments" name="ID" />
<button type="submit">Go</button>
</form>
So now check only the parameter value, if it is there:
[HttpPost]
public ActionResult Index(int ID)
{//Put a breakpoint here and check if ID has value
return View();
}
If the value exists there, than assume that it is okay.
Here is a screenshot that I have tested this answer.

Update a field in view based on the model in ASP.NET MVC

I need to do a calculator in ASP.NET MVC.
For the beginning I want to receive the value from the input field in controller and prefix it with the string "123". At the end I will process the expresion received and return the result.
I have the following model:
namespace CalculatorCloud.Models {
public class Calculator
{
public string nr { get; set; }
} }
In the view I am using the model:
#model CalculatorCloud.Models.Calculator
#{
ViewBag.Title = "Calculator";
}
#using (Html.BeginForm("Index", "Home"))
{
<div>
<div class="header">
#Html.TextBoxFor(m => m.nr, new { #id = "nr"})
<input type="button" id="C" name="C" value="C" />
<input type="button" id="back" name="back" value="<-" />
[...]
<div class="sum">
<input type="submit" value="=" />
</div>
</div>
}
The controller is like this:
namespace CalculatorCloud.Controllers
{
public class HomeController : Controller
{
Calculator model = new Calculator();
public ActionResult Index(string nr)
{
model.nr = "123" + nr;
return View(model);
}
}
}
I have the following problem: when pressing on submit button I am expecting to be displayed on the textbox the value from that was previously in the textbox, prefixed with the string "123".
But now it is kept the value from the textbox without the string "123".
Can someone help me with this?
Thank you! :)
If you want to modify the value of a model property in a postback action you will need to remove it from the ModelState:
public ActionResult Index(string nr)
{
ModelState.Remove("nr");
model.nr = "123" + nr;
return View(model);
}
The reason for this is that Html helpers such as TextBoxFor will first look at the value present in the ModelState and then in your view model property when rendering the value. This is by design.

Get Values of DropDownList which is inside webgrid and also each dropdown is an attribute of the model

I have a webgrid contains a dropdown which contains different items for each user(Items are grouped). I want to get the selected values to the controller . How can I do that. Heres my ;
Model :
public SelectList AvailableDevices { get; set; }
View :
...
var grid = new WebGrid(Model. ...
..
..
grid.Column(header: "AvailableDevices", format: #item => Html.DropDownList("value", (IEnumerable<SelectListItem>)item.AvailableDevices)),
And I have a Submit Button
#using (Html.BeginForm("AssignUserDevices", "Device"))
{
<input type="submit" value="setUserDevice" onchange="CheckSelectedDevices()" />
}
I want to set users device according to his user type. I know what his choices and send dropdown items according to his type. So each item in webgrid differs from each other.
And Also I dont know how to give indices to each item in webgrid.( I think we will need it.)
Im new at MVC so hope you will understand.
Thanks;
What I got from our requirement that you want the selected item and have that item value in form field before posting if yes then you can follow as given.
#Html.DropDownList("value", (IEnumerable<selectlistitem>)item.AvailableDevices), new {#class="deviceclass"} )
#using (Html.BeginForm("AssignUserDevices", "Device"))
{
<input type="hidden" value="" name="deviceId" id="deviceId" />
<input type="hidden" value="" name="userId" />
<input type="submit" value="setUserDevice" />
}
<script>
$(".deviceclass").on('change', function () {
var dropdownvalue = $(this).val();
$('#deviceId').val(dropdownvalue);
})
</script>
and you can define an action function in controller
public ActionResult Details(string deviceId, string userId)
{
// do as you need.
return View();
}

How To Pass Value Entered In A Text Box To An Action Method

I was building a Movies application using MVC. CRUD was automatically created for me by Visual Studio. Now, I am trying to build a Search functionality for the user. Here is the code I wrote:
#using (Html.BeginForm("SearchIndex", "Movies", new {searchString = ??? }))
{
<fieldset>
<legend>Search</legend>
<label>Title</label>
<input type ="text" id="srchTitle" />
<br /><br />
<input type ="submit" value="Search" />
</fieldset>
}
I have built the SearchIndex method and the associated view. I just can't find how to pass the value entered in the text box to the SearchIndex action method.
Please help.
In your Model:
public class Search
{
public String SearchText { get; set; }
}
Make your View strongly typed and use
#Html.EditorFor(model => model.SearchText)
In your Controller:
[HttpPost]
public ActionResult SearchIndex(Search model)
{
String text = model.SearchText;
}
Hope this helps.
You need to give your input field a name:
<input type="text" id="srchTitle" name="movieToFind" />
Then in your Controller make sure it has a string parameter:
in MoviesController:
[System.Web.Mvc.HttpPost]
public ActionResult SearchIndex(string movieToFind)
{
//Controller Action things.
}
Note: Form fields names must match the parameters expected in the controller. Or map to model properties if a 'Model' is expected.

MVC - two text box with the same parameter

#using (Html.BeginForm("Orders1", "Track", FormMethod.Post))
{
#Html.TextBox("order")<br />
#Html.TextBox("order")
<input type="submit" value="Submit" />
}
Hello how can i use two text box and pass value to the same parameter? The first textbox work but the other textbox doesn't.
public ActionResult Orders1(IEnumerable<int> order)
{
var query = from a in context.CM_Checkout_Details
where a.CheckoutDetails_ID == order // my error
select a;
return View(query);
}
}
simple solution would be to change your parameter from int to IEnumerable<int>
public ActionResult Orders1(IEnumerable<int> orders)
{
}

Resources