ASP .Net MVC 2 custom Helper Grid not posting Model to controller action - asp.net-mvc-2

I've created a custom helper that renders a grid and receives the strongly typed view's model as a parameter.
Basically my view looks like this:
<% using (Html.BeginForm("UpdateValues", "Home", FormMethod.Post)) { %>
<%= Html.MyGrid(Model)%>
<input type="submit" value="Update Values" />
<%} %>
But when I click on the submit button, all the values on the model are null.
This is what the controller looks like:
[HttpPost]
public string UpdateValues(AssignmentResultsVm assignmentResults)
{
//..... do something
}
How can I make this work?
Thanks in advance.

You need to make sure that the items are arranged in your grid so that the default model binder can map the data map to your view model class.
This is done by index the name property of the data your are binding like this:
<form method="post" action="/Home/Create">
<input type="text" name="[0].Title" value="Curious George" />
<input type="text" name="[0].Author" value="H.A. Rey" />
<input type="text" name="[1].Title" value="Code Complete" />
<input type="text" name="[1].Author" value="Steve McConnell" />
<input type="submit" />
</form>
You can have this done for you by the EditorTemplates feature of asp.net mvc which is exemplified in this article.

Related

Form not submitting with query parameters set with "asp-route" despite being in action

UPDATE: per this response this is behavior of HTML5? I tried setting the parameters in hidden inputs and that works. Doesn't make much sense to me, but what do I know.
I have my form for a "next page" button set up like this:
<form id="next" method="get"
asp-controller="Search"
asp-action="Cities"
asp-route-sortOrder="#ViewData["CurrentSort"]"
asp-route-currentFilter="#ViewData["CurrentFilter"]"
asp-route-pageNumber="#(Model.PageIndex + 1)">
<input form="next" type="submit" class="page-btn" disabled="#nextDisabled" value="Next" />
</form>
When I inspect the page, the form has the correct action url (example):
/Search/Cities?currentFilter=Test&pageNumber=2
But the request actually being made is to
/Search/Cities?
But when it hits the controller, all of the parameters are null. Here is the top of my controller:
[Route("Search/Cities")]
public ActionResult Cities(string SearchString, string sortOrder, string currentFilter, int? pageNumber)
I'm not sure what I'm missing here.
you have 3 choices. This code was tested
first the most popular, you have to use post
<form id="next" method="post"
asp-controller="home"
asp-action="test"
asp-route-sortOrder="CurrentSort"
asp-route-currentFilter="CurrentFilter"
asp-route-pageNumber="1">
<label for="searchString">Search</label>
<input type="text" id="searchString" name="searchString"><br><br>
<input form="next" type="submit" class="page-btn" value="Next" />
</form>
in this case searchingString will be sent in a request body, all another params in a query string
second
<a href="/Home/Test?sortOrder=CurrentSort&currentFilter=CurrentFilter&pageNumber=2">
<button class="page-btn">Next</button>
</a>
the second option will generate get request if it is so important for you, but you will not be able post search string except using ajax.
third, you can use get, but route params should be in the inputs, probably hidden, search string will have a visible input
<form id="next" method="get"
asp-controller="home"
asp-action="test">
<input type="hidden" id="sortOrder" name="sortOrder" value="CurrentSort" />
<input type="hidden" id="currentFilter" name="currentFilter" value="CurrentFilter" />
<input type="hidden" id="pageNumber" name="pageNumber" value="23" />
<label for="searchString">Search</label>
<input type="text" id="searchString" name="searchString"><br><br>
<input form="next" type="submit" class="page-btn" value="Next" />
</form>
in this case nothing will be inside of the body, everything in a query string including searchString.

How to pass data from a form in a view to another controller with C# MVC?

If I have a form that needs to use a textbox input like as below:
#{
if(IsPost){
username = Request.Form["username"]
}
}
<form action="Home/Index" method="post">
<input type="text" name="username" />
<input type="submit" value="submit" />
</form>
The controller is something like below,
public class HomeController : Controller
{
public ActionResult Index (string username) {
if (string username != string.Empty)
{
Console.WriteLine("Your username is " + username);
}
return View();
}
}
Seems like the data is not being passed from the post method. When I hit submit the URL that it requests is Home/Home/Index, which is not were the controller(HomeController) action is located, it should be Home/Index, and use the HomeController right?
What if I need to pass this data to a different controller that has an Index for the action, like UserController?
In this instance, you're missing a slash!
<form action="/Home/Index" method="post">
<input type="text" name="username" />
<input type="submit" value="submit" />
</form>
But when using asp.net mvc, you should use Url.Content with the home directory character to ensure that if your site is deployed in a sub-directory of the site, the correct root will be found. So use:
<form action="#Url.Content("~/Home/Index")" method="post">
<input type="text" name="username" />
<input type="submit" value="submit" />
</form>

MVC how can i post a form without using a model

I would like to know how i could sumbit a form in mvc 4 without using a model binding to the view.
can some one please demontrate how? and how can i use that form in the controller?
It is very easy to use a form without any sort of Model binding.
Simple set up your for using either #using(Html.BeginForm()){ ... } or plain html.
<form id="myForm" action="/ControllerName/ActionName" method="Post">
<input type="text" name="foo" value="FOOOO" />
<input type="text" name="bar" value="Baaaar" />
<button type="submit">Submit</button>
</form>
When you post this to the ActionResult, your action result just needs to be set up to accept a string value named foo and one named bar
[HttpPost]
public ActionResult ActionName(string foo, string bar){
// your code here
return View();
}

MVC4 Pass Model from View to Controller

//Model
public class Car
{
string name{get;set;}
double speed {get;set;}
//etc.
}
Basic model
//Controller
public ActionResult ModifyCar(Car c)
{
c = GetAdditionalData(c.name, c.speed);
return View(c);
}
So I'm making call to outside service to get additional data for car.
Below is the form which I'm really confused on. I am just going to write this in HTML.
But from here I'd like to take a basic empty model.. populate it then send to the controller for additional data.
//View
#model Car
<form id="carform" method="post" action="/Car/ModifyCar"> //Guessing
<input id="carName" /> //Somehow bind with #model.name
<input id="carSpeed" /> //Somehow bind with #model.speed
<input type="submit" /> //Somehow send the model to the controller
</form>
Sorry I'm not really good at the View part at all but I hope you can see where I was going here.
Also, I'm doing validation that requires a form id.. I noticed that there is no form id when using HTML Helper to make the form. Thats going to break my CSS I wrote.
Any help would be greatly appreciated.
Thanks
Try this :
#model Car
<form id="carform" method="post" action="/Car/ModifyCar"> //Guessing
<input id="carName" name="carName" value=#Model.name /> //Somehow bind with #model.name
<input id="carSpeed" name="carSpeed" value=#Model.speed /> //Somehow bind with #model.speed
<input type="submit" value="Submit" /> //Somehow send the model to the controller
</form>
when your form will submit it will go to ModifyCar action in Car
Controller.
But as you has mentioned in your question that we cannot give id in html helper ,it is wrong we can for ex:
#using(Html.BeginForm("ModifyCar","Car",FormMethod.Post,new{ id="carform" }))
the above BeginForm() will render same html as:
<form id="carform" method="post" action="/Car/ModifyCar">

MVC Form in MasterPage not working

Ok, This is a bit confusing and frustrating.
Here's what I have in my masterpage :
<%using (Html.BeginForm("Index", "SearchController", FormMethod.Post, new { #name = "dosearch" }))
{%>
<input type="text" name="ssearch" class="search"><input id="Image1" type="image" runat="server" src="~/App_Themes/DefaultTheme/images/btn_search.gif" width="74" height="29" style="border:none" />
<%} %>
But the corresponding generated html is:
<form action="" method="post" name="dosearch">
<input type="text" name="ticketSearch" class="search"><input src="../App_Themes/DefaultTheme/images/btn_search.gif" name="ctl00$Image1" type="image" id="ctl00_Image1" width="74" height="29" style="border:none" />
</form>
Question is WHY action is empty whereas I have mention action and controllername when declaring the HTML.Helper ????
as a reasult, the seach is not working for obvious reason.
Please help. Thanks.
I think your problem is that you are using "SearchController" but you only need to use "Search" as the MVC framework will add the word "controller".
Also, not that it matters but you are missing a closing input tag