Passing data from View to Controller - c#

In an ASP.NET MVC application, I'm making logic for Admin to accept or reject new members. I'm showing a list of members and two buttons Accept and Reject, like this:
<% foreach (var mm in (ViewData["pendingmembers"] as List<MyMember>)) %>
<% { %>
<tr><td>Username:<%=mm.UserName %></td><td>
<tr><td>Firstname:<%=mm.FirstName %></td><td>
...etc...
<tr>
<td>
<% using (Html.BeginForm("AcceptPendingUser", "Admin"))
{ %>
<input type="submit" value="Accept" />
<% } %>
</td>
<td>
<% using (Html.BeginForm("RejectPendingUser", "Admin"))
{ %>
<input type="submit" value="Reject" />
<% } %>
</td>
</tr>
<% } %>
So, the list of pending member data is in a list of MyMember-objects. Each MyMember object will be printed out member and two buttons are setup for the admin to either accept or reject a pending member.
Then, in the controller I'm separating the handling of those two input fields/forms, like this:
public ActionResult AcceptPendingUser()
{
// TODO: Add code to save user into DB and send welcome email.
return RedirectToAction("Index");
}
public ActionResult RejectPendingUser()
{
// TODO: Add code to remove user from PendingUsers list and send rejection email.
return RedirectToAction("Index");
}
I would like to directly get the object next to the button the user pressed.
How can I send the MyMember object from the View to the controller?
Or how do I send perhaps a numeric index with button press? Maybe with a hidden field?

The simplest option would probably be a hidden input:
<input type="hidden" value="<%=mm.Key%>" name="key" id="key" />
(name accordingly; in each form)
The two controller would then take an argument called "key" (rename to suit). If you want to parse the object from multiple inputs, you'll need a ModelBinder. Of course, rather than 2*n forms, you might consider either query-string based urls, or use something like jQuery (or some other script helper) to submit the data without needing the forms (if script is available).

Instead of using an HTML button consider using an ActionLink and construct it to include the id of the member being approved. Another alternative would be to have a checkbox (whose value is the id of the member being approved) that the admin can select for each member to be approved and a similar one for reject and one each approve/reject buttons for the entire form.

Answering to myself and other mvc newbies:
I got it finally working with this code:
VIEW:
<%=Html.ActionLink(
"Jump",
"Jump",
new { name=(ViewData["Person"] as Person).Name,
person=ViewData["Person"]},
null) %>
CONTROLLER:
public ActionResult Index()
{
ViewData["Title"] = "Home Page";
ViewData["Message"] = "Welcome to ASP.NET MVC!";
Person p = new Person();
p.Name = "Barrack";
p.Age = 35;
ViewData["Person"] = p;
return View();
}
public ActionResult Jump(string name, Person person)
{
return View();
}
Debugging the app in the Jump method gives me nice "Barrack"-string for the name parameter, but Person parameter in null.
I also understand what the kind commenters tried to explain: it's easy to send simple data types like strings and ints to controller, but complex types such as my Person object needs something else.
Basically passing an int is enough for me. The hardest part here was figuring out the right way to set up ActionLink.
Cheers,
Pom

Related

MVC Maintaining Model through multi view form submits

So I am working on a MVC which is basically three steps.
Create a view for each step i.e.
StepOne
StepTwo
StepThree
On step one and two I ask the users to enter some details.
All the values for the multiple step I store in one Model.
And getting from StepOne to StepTwo is fine. Certain values in my model are being set and maintained.
But on StepTwo when I do my second httppost and pass the model, it seems to just create a new instance of the model and values from stepone are not maintained.
<% using (Html.BeginForm("StepTwo", "Home", FormMethod.Post, new { id = "restrictionForm" })) { %>
<%: Html.AntiForgeryToken() %>
<div id="wrapping" class="clearfix">
<h3>Postcode Restriction Type : </h3>
<%= Html.DropDownListFor(x => x.SelectedRestriction, Model.RestrictionTypes,"Select Restriction...", new { #class = "selmenu required" }) %>
<h3>Restriction Description : </h3>
<%= Html.TextBoxFor(m => m.RestrictionDescription, new { #class = "txtblock required" }) %>
</div>
<section id="buttons">
<input type="submit" value="Submit" id="submitBtn" />
</section>
And in my controller
On Page Load my Model is still intact and still maintains values from previous step.
[Authorize]
public ActionResult StepTwo(PostcodesModel model)
{
var summaryMessage = "";
model.SummaryMessage = summaryMessage;
model.RestrictionTypes = _Provider.GetRestrictionTypes();
return View(model);
}
But at the Httppost, the model has lost values and seems to have created new instance of model.
[Authorize]
[HttpPost]
[ActionName("StepTwo")]
[ValidateAntiForgeryToken]
public ActionResult StepTwoPost(PostcodesModel model)
{
return View(model);
}
Any idea how I can maintain model between Http Posts ?
It seems from your question that you believe models persist across requests. This is not true.
You either pass information to the view via your model from the controller, or submit values from your view to your controller and MVC handles this by binding html form inputs to your View Model.
If you want to persist your View Model across each step you need to take the values accepted and copy them into a new model (or directly inject it) when calling your new view.
Something like this (I just typed this up off my head so its not clean but should give you an idea):
[HttpGet]
public ActionResult StepOne()
{
var model = new MyNewModel();
return View(model);
}
/* NOTE THE MODEL PASSED BACK HERE IS NOT THE EXACT SAME OBJECT
AS THE ONE CREATED IN THE GET ACTION ABOVE, MODEL BINDING HAS OCCURRED
TO READ YOUR FORM INPUTS AND MATCH THEM TO A NEW MODEL WHICH IS EXPECTED */
[HttpPost]
public ActionResult StepOne(MyNewModel model)
{
if (ModelState.IsValid)
{
// Do something here
// pass model to new view
TempData["model"] = model;
return RedirectToAction("StepTwo");
}
return View(model);
}
[HttpGet]
public ActionResult StepTwo()
{
MyNewModel model;
if (TempData["model"] != null)
{
model = (MyNewModel) TempData["model"];
// make some changes if necessary
model.MyProperty = 2;
return View(model);
}
return RedirectToAction("StepOne");
}
I think you can also keep your model in Session ( per application ) or in a ViewState ( per page ).
Every time you make a post you upgrade the session. It's also optimal because on the client side you receive only a session identifier.
Some differences between Session and Viewstate:
Session is per application, while ViewState is per page
Session sends to the client side only a session identifier, while ViewState sends an ecrypted text

html form posting to mvc controller

I am trying to set up a simple login html page, whose action is sent to mvc controller on another of my sites. I have no problem setting up the page to do the post, and in the mvc controller I have my method that reads the form post. The problem is that I am not seeing my fields from the html form in the form collection.
Is there something special that I need to do to read a form post within a mvc controller method, if so what is that?
The is the form action markup from my page
<form action="http://reconciliation-local.sidw.com/login/launch" method="post">
User Name <input type="text" id="username"/><br/>
Password <input type="text" id="password"/>
<input type="submit" value="launch"/>
</form>
The controller method
[HttpPost]
public ActionResult launch(FormCollection fc)
{
foreach (string fd in fc)
{
ViewData[fd] = fc[fd];
}
return View();
}
When I step through the controller method code, I am not seeing anything in the formcollection parameter.
Post Html To MVC Controller
Create HTML page with form (don't forget to reference a Jquery.js)
<form id="myform" action="rec/recieveData" method="post">
User Name <input type="text" id="username" name="UserName" /><br />
Password <input type="text" id="password" name="Password"/>
<input type="submit" id="btn1" value="send" />
</form>
<script>
$(document).ready(function () {
//get button by ID
$('#btn1').submit(function () {
//call a function with parameters
$.ajax({
url: 'rec/recieveData', //(rec)= Controller's-name
//(recieveData) = Action's method name
type: 'POST',
timeout: '12000', (optional 12 seconds)
datatype: 'text',
data: {
//Get the input from Document Object Model
//by their ID
username: myform.username.value,
password: myform.password.value,
}
});
});
});
</script>
Then in The MVC Controller
controller/action
| |
1. Create Controller named rec (rec/recieveData)
Create View named rec.cshtml
Here is the controller:
public class recController : Controller
{
// GET: rec
string firstname = "";
string lastname = "";
List<string> myList = new List<string>();
public ActionResult recieveData(FormCollection fc)
{
//Recieve a posted form's values from parameter fc
firstname = fc[0].ToString(); //user
lastname = fc[1].ToString(); //pass
//optional: add these values to List
myList.Add(firstname);
myList.Add(lastname);
//Importan:
//These 2 values will be return with the below view
//using ViewData[""]object...
ViewData["Username"] = myList[0];
ViewData["Password"] = myList[1];
//let's Invoke view named rec.cshtml
// Optionaly we will pass myList to the view
// as object-model parameter, it will still work without it thought
return View("rec",myList);
}
}
Here is the View:
#{
ViewBag.Title = "rec";
}
<h2>Hello from server</h2>
<div>
#ViewData["Username"]<br /> <!--will display a username-->
#ViewData["Password"] <!-- will display a password-->
</div>
If you posted some code it would be much easier to help you, so please edit your question...
Make sure that your form's action has the correct address, that your method is specifying POST (method="POST") and that the input fields under your form have name attributes specified.
On the server side, try making your only parameter a FormCollection and test that the fields in your form posted through the debugger. Perhaps your model binding isn't correct and the FormCollection will at least show you what got posted, if anything.
These are just common issues I've seen. Your problem could be different, but we need to see what you're working with to be able to tell.
Try something like this:
cQuery _aRec = new cQuery();
_aRec.Sqlstring = "SELECT * FROM Admins";
DataSet aDS = _aRec.SelectStatement();
DataTable aDT = aDS.Tables[0];
foreach (DataRow aDR in aDT.Rows){
if (txtAdminUsername.Text == aDR[0].ToString()){
if (txtAdminPassword.Text == aDR[1].ToString()){
Session["adminId"] = aDR[0];
Response.Redirect("Admin.aspx");
return;
}
}
}
Make sure that your FormCollection object properties for username and password are defined properly.
I had to use the name attribute on the text tag, and that solved my problem, is now working like a charm.
You have to use Ajax to do that.. Whenever you want to "submit" from client side, you should use Ajax to update the server
Step 1 - you redirect your Ajax call to your action, but with your list of parameters in the query-string appended
$.ajax(url: url + "?" + your_query_string_parameter_list_you_want_to_pass)
Step 2 - add optional parameters to your Controller-action with the same names and types you expect to get returned by the client
public ActionResult MyControllerAjaxResponseMethod(type1 para1 = null,
type2 para2 = null,
type3 para3 = null, ..)
Know that the optional parameters have to be initialized, otherwise the Action itself will always ask for those
Here's where the "magic" happens though --> MVC will automatically convert the query-string parameters into your optional controller-parameters if they match by name
I was also looking for a good answer for this, --> i.e. - one that doesn't use q-s for that usage, but couldn't find one..
Kinda makes sense you can't do it in any other way except by the url though..

MVC navigate back to page with same model

I've built a Search Page with 5 properties to filter on.
When a user clicks on one of the results the detail page is loaded.
Now I want to provide a "Back" button so the user can go back to the Search Page with the original filter.
I was thinking about using TempData to store the filter model.
Tempdata is stored in session for only one call so the session won't be bloated after a while.
Is there a better sollution or do you guys have some suggestions?
Let me know!
Edit:
The search page will make use of ajax calls to page, sort or filter the data.
So all this data will need to be stored if I want to navigate back from the detail page.
Is TempData the best way?
Why not to use query-string for this? E.g. search request is submitted using <form /> element with method attribute set to "get". In this case you can easily restore the form state by just reading from the query-string, the code will be much simpler. Visitors also can easily bookmark the page and return to search results later.
View:
#model SearchResultSet;
<form method="get" action="/search">
<input type="text" name="q" value="#Request.QueryString["q"]" />
<input type="submit" value="Search" />
</form>
#if (Model.Total > 0)
{
<ul>
#foreach (var result in Model.Results)
{
<li>...</li>
}
</ul>
}
Model & controller:
public class SearchResultSet
{
public IList<SearchResult> Results { get; set; }
public long Total { get; set; }
}
public class SearchController : Controller
{
public ActionResult Index(string q = "")
{
return View(GetModel(q));
}
private SearchResultSet GetModel(string searchQuery)
{
// Get search results
}
}
Hope this helps.
Well, TempData retains value for one call, but you can retain the TempData value using TempData.Keep() until your Session expires.
TempData["YourKey"] = "SomeValue";
TempData.Keep("YourKey");
Hope it helps.

How do I return two different Views from the same Action in ASP.NET MVC?

I have two views which will both use the same Controller method:
//webServiceController.cs
//The actual method is about 40 lines of code. Truncated for readability.
public ActionResult Index()
{
object i = new List<WebServiceMethod>();
i = svcService.populateList("Programs");
return View(i);
}
The first view is an HTML page that displays the data in a pretty table output:
<% // Index.aspx %>
<table>
<tbody>
<% foreach (var item in Model) { %>
<tr>
<td>
<% if (Convert.ToInt32(item.numberRequests) > 0)
{%>
<%= Html.ActionLink("Details", "Details", new { programNumber = item.programNumber })%>
<%} %>
</td>
<td>
<%= Html.Encode(item.programNumber) %>
</td>
</tr>
<% } %>
</tbody>
</table>
The second view is a quick'n'dirty conversion to JSON so that I can do magical AJAX tricks with the data:
<%
// AjaxGetServiceData.aspx
// Convert web service response object into JSON for AJAX.
var jss = new System.Web.Script.Serialization.JavaScriptSerializer();
Response.Write(jss.Serialize(Model));
%>
I'd created a duplicate of the Index() method and called it AjaxGetServiceData(), but that defeats the purpose of MVC.
Resolution:
I didn't ask my question very well, as evidenced by a 5-10 minute discussion I just had with a coworker about this very topic. He kept asking me the same question that many users on this page asked me: "How does the controller know which view to return?" I responded, "That's what I'm trying to figure out." I was trying to get the method to return a different view (or Json output) when AJAX was the requester. A string argument in the method was my solution.
This is what I ended up using to get my desired effect:
public ActionResult Index(string isJSON = "no")
{
/// ...All the code from before...
if (isJSON == "yes")
{
return Json(i, JsonRequestBehavior.AllowGet);
}
else
{
return View(i);
}
}
Then, when I want the JSON version, in my AJAX request I specify the URL as /MyController/Index/?isJSON=yes
When I want my pretty table view, I just use /MyController/
public ActionResult Index()
{
object i = new List<WebServiceMethod>();
i = svcService.populateList("Programs");
if (someCondition)
return View(i);
else
return View("AjaxGetServiceData", i); // or whatever you called your view.aspx
}
It sounds like you have two different purposes in which case I think you are going the right way when you talk about different controller methods.
Sure, reuse code inside each controller method but if you want a different result, use a different method and keep the controller methods simple.
" ...I was trying to get the method to return a different view (or Json output) when AJAX was the requester..."
public ActionResult Index()
{
object i = new List<WebServiceMethod>();
i = svcService.populateList("Programs");
if (Request.IsAjaxRequest == "True")
{
return Json(i, JsonRequestBehavior.AllowGet);
}
else
{
return View(i)
}
}

Multiple actions on the same controller and view in asp.net MVC

How do I use multiple actions on the same controller?
I'm using the default project that comes up when opening a new project in asp.net mvc.
I added one more Index action on the homecontroller to accept a value from a textbox...like this
string strTest;
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection frm)
{
strTest = frm["testbox"];
return RedirectToAction("Index");
}
Now,I need to display the entered value back to the user. How do I do this?
I tried this..
public ActionResult Index()
{
this.ViewData.Add("ReturnMessage", strValue);
return View();
}
Here's what I've put on my view..
<% using (Html.BeginForm())
{ %>
<p>
<%=Html.TextBox("testbox")%>
</p>
<p>
<input type="submit" value="Index" /></p>
<p>
<%= Html.ViewData["ReturnMessage"] %>
</p>
<% } %>
the compiler typically doesn't let me add another index with same constructor to display the entered message back to the user which is obvious in c# I know. But,then how do I get the message back out to the user.
Thanks
Well, a controller matches one route, based on the parameters sent. You can layer your routes from most specific to least specific, it checks in order. First one that hits wins.
The other answer is to either strongly type your model sent to your view, or store it in the ViewData:
ViewData["Message"] = "Welcome to ASP.NET MVC!";
Then access it in your View:
<%= Html.Encode(ViewData["Message"]) %>
Simple method
In your view
<% using (Html.BeginForm()) {%>
<%= Html.TextBox("myInput") %>
<%= ViewData["response"] %>
<%}%>
In your controller;
public ActionResult Index()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection collection)
{
ViewDate.Add("response", collection["myInput"]);
return View();
}
Josh, see the previous question you asked.
In there I had <%= Html.textbox("myInput", Model.myInput....
it's the Model.myInput that will put the value from your model into the text of yoru text box.
EDIT
Or if you don't want it in a text box then simply do;
EDIT 2
You can add as many items into your new form view model and it has, in this case, nothing to do with a database. see your previous question on where i declared the class.
the class can have as many properties as you like. So you can add a string myResponse {get;set;} to return a response back to your view so then you can use <%=Model.myResponse%>
Hope this helps.

Categories