List is NULL while POST from View - c#

I use the below Action to allow the user to see a preview of Excel import
[HttpGet]
[Authorize]
public ActionResult ImportVerify()
{
string temp_sessionname = "ImportedData_" + User.Identity.Name;
List<ProjectImportModel> view_model = (List<ProjectImportModel>)TempData[temp_sessionname];
return View(view_model);
}
[HttpPost]
[Authorize]
public ActionResult ImportVerify(List<ProjectImportModel> model)
{
return View(model);
}
And on View i am using a table to show the List of imported data from excel and ask user to confirm the action of import
My view is like this
<h2>Import Verify</h2>
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
//table with all details and a submit button in the end
<div class="form-group">
<div class="col-md-10" style="text-align:center;">
<input type="submit" value="Submit" class="btn btn-primary" />
</div>
</div>
}
And model is
public class ProjectImportModel
{
public string Page { get; set; }
public string Author { get; set; }
public string Translator { get; set; }
public string Holder { get; set; }
public string Title { get; set; }
public string TrTitle { get; set; }
//and similar 20 more properties of string type
}
But on POST the list is null
Is any way to get the list back at POST event. My intension is just to allow the preview to user
Or do i need to refill List from TempData # post as well?

In order to post a collection back you need to index the properties, if they're readonly you can just use HiddenFor with a model.
If you want the user to edit them, change them to TextBoxFor's instead or the control that you data requires.
#model List<ProjectImportModel>
<h2>Import Verify</h2>
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
//table with all details and a submit button in the end
<div class="form-group">
<div class="col-md-10" style="text-align:center;">
#for(var i = 0 ; i < Model.Count; i++)
{
#Html.HiddenFor(m => m[i].Foo)
#Model[i].Foo <br/>
}
<input type="submit" value="Submit" class="btn btn-primary" />
</div>
</div>
}
I just used a dummy property of Foo without seeing your model.
Obviously you would want to display the data too.
Model Binding To A List

I don't see the snippet code that set your TempData. So I intend that you set it in another Action and then redirect to the ImportVerify Action
TempData keep the information for the time of an HTTP Request. This mean only from one page to another. It's mean that after redirect to your ImportVerify Get Action the data is expired. If you want to keep the data you can try the following ways:
Create a hidden List in your page
Use Session instead of TempData

There are some requirements to post a list back to a controller. Specifically, indexes must be 0-based and unbroken (example, you can bind a list with indexes [0], [1], [2] but not one with [0], [1], [3] or [1], [2], [3].
Alternatively, you could write a custom model binder to parse the request body the way you like.
More on this here: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/
EDIT:
Here is an example on how to do it:
Given the following model
public class ExampleModel
{
public int Field1 {get; set;}
public string Field2 {get; set;}
}
And the following actions
[HttpGet]
[Authorize]
public ActionResult ImportVerify()
{
List<ExampleModel> model = //populate the list somehow
return View(model);
}
[HttpPost]
[Authorize]
public ActionResult ImportVerify(List<ExampleModel> model)
{
//do something
}
The example view "ImportVerify.cshtml" would be:
#model List<ExampleModel>
#using(Html.BeginForm())
{
for(var i = 0; i < Model.Count; i++)
{
<div>
#Html.HiddenFor(m => m[i].Field1);
#Html.HiddenFor(m => m[i].Field2);
<p>
Value #i : Field1 = #m[i].Field1 , Field2 = #m[i].Field2
</p>
</div>
}
<input type="submit" value="Send"/>
}
Also, I would revise your logic a bit, to avoid the use of TempData (generally bad practice) and to allow strongly typed views.

Related

List can only store one item in MVC?

Im struggling to pass data from view into a list in the controller.
I can add 1 item to the list, but when the controller goes back to the view to recieve another input from the user, the previous item in the list is replaced with the new one. The last item just isnt there anymore, so the list always has just 1 item in the count. Its almost like it just resets everytime the user input a new string. As if it cant store more than one string at a time.
I want to store multiple user inputs in the AddTagVM.StringList, so that I can eventually retrieve and print them in a view.
This is my model:
public class AddTagVM
{
public Post Post { get; set; }
public List<Tag> TagList { get; set; }
public List<string> StringList { get; set; } = new List<string>();
public string TagName { get; set; }
}
This is the controller:
public ActionResult AddTag()
{
AddTagVM tagg = new AddTagVM();
return View(tagg);
}
[HttpPost]
public ActionResult AddTag(AddTagVM tagg)
{
//tagg.Tag[tagg.Add].Name = tagg.TagName;
tagg.StringList.Add(tagg.TagName);
return View(tagg);
}
And this is the view:
#model BlogNiKRaMu.Models.AddTagVM
#{
ViewBag.Title = "AddTag";
}
<h2>AddTag</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Post</h4>
<hr />
#Html.EditorFor(model => model.TagName)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
You can remove = new List<string>(); from the VM first as see if it works as expected.
Else try to do it like this.
[HttpPost]
public ActionResult AddTag(AddTagVM tagg)
{
var tag = tagg.TagName; // <---Extract the value to a variable first.
tagg.StringList.Add(tag);
return View(tagg);
}
When you post back the form, your AddTagVM object will only have TagName input not the list value as you lose the list. Meaning, when post the form, the request should contain all the values you need to retain in the object i.e the StringList should also be posted along with string.
So the work around you can try is, you can add HidderFor to the StringList. This way when the form post the object will have value for TagName and StringList as well.
#Html.HiddenFor(model => model.StringList)
#for(int i= 0; i<StringList.count(); i++)
{
#Html.HiddenFor(model=>model.StringList[i])
}

what should be parameters of action method when there is foreach loop in your form in view mvc

I am working on a project "Online examination system". User can give MCQ test here.
Now in my database I have a table named "Question" which contains questions of every subject in the database. When the user selects a subject for an exam, the questions of that subject are stored in var a by where query. Now that var a is passed to return view.
Then in the Exam view, the name of every field is dynamic and differs by ID column in table which is auto increment primary key.
Every MCQ is submitted to Exam_Result() Action method for Checking that its right or wrong.
Now the confusion is in Exam_Result() Action method's parameters... say 20 questions are submitted to Exam_Result, andn how should I handle dynamic names in the parameter of Exam_Result() ?
Exam() Action method in home controller:
public ActionResult Exam(string sname)
{
ProjectDatabaseEntities7 obj = new ProjectDatabaseEntities7();
Question q = new Question();
try
{
var a = obj.Questions.Where(s=>s.Subject_Name.Equals(sname));
return View(a);
}
catch
{
}
return View();
}
Exam.cshtml View:
<form action="/User/Exam_Result">
#foreach (var s in Model)
{
<div class="col-md-12 well form-group">
<label>#s.Question1</label><br>
<input type="text" name="question{#s.Id}" value="#s.Question1" hidden/>
<input type="radio" name="option{#s.Id}">#s.Option1<br />
<input type="radio" name="option{#s.Id}">#s.Option2<br />
<input type="radio" name="option{#s.Id}">#s.Option3<br />
<input type="radio" name="option{#s.Id}">#s.Option4<br />
</div>
}
<input type="submit" value="Submit" class="btn btn-success btn-group-justified" />
</form>
Exam_result() Action Method where the form above is submitting:
public ActionResult Exam_Result(string question, string option)
{
ProjectDatabaseEntities7 obj = new ProjectDatabaseEntities7();
Question q = new Question();
try
{
q = obj.Questions.First(x=> x.Question1.Equals(question));
if(q!=null)
{
if (q.Answer == option)
ViewBag.ans = "right ans";
else
ViewBag.ans = "wrong ans";
}
}
catch
{
return View();
}
return View();
}
Database table Question:
Exam_Result can accept your model as a parameter, allowing it to accept anything that your model defines. When you call into your exam function, and you give it your model, you're giving it all of the properties that it needs at once.
public ActionResult Exam_Result(YourModelClass model)
In your model, you'll just have a property that contains a list of the property that you need N of.
public class YourModelClass
{
public int ExamId {get; set;}
public List<Type> ListName { get; set; }
}
You can access the list as it is a property of the model in your view:
#foreach(var listItem in model.ListName)
{
#listItem
}
To send this information back to the view, you can just return it explicity:
return View("ViewName", model);
Since you're using Razor, all you need to do to define the model in your view is add this to the top of the page:
#model Your.Namespace.YourModelClass
For your comment about having different names and values:
public class YourCustomType
{
public string Name {get; set;}
public var WhateverElse { get; set; }
}
#foreach(YourCustomType item in model.YourList)
{
//item.Name
//item.WhateverElse
}
You model binding is wrong.
First of all don't use entity objects in View page, instead create a custom View model and map entity objects and bind it to view page.
You form <form action="/User/Exam_Result"> submits a list of Model (question) but your action only accepts one question (question/option parameters) which is wrong. You should change Exam_Result action parameter to accept a list of questions

MVC 4 ViewModel not being sent back to Controller

I can't seem to figure out how to send back the entire ViewModel to the controller to the 'Validate and Save' function.
Here is my controller:
[HttpPost]
public ActionResult Send(BitcoinTransactionViewModel transaction)
{
}
Here is the form in the view:
<li class="check">
<h3>Transaction Id</h3>
<p>#Html.DisplayFor(m => m.Transaction.TransactionId)</p>
</li>
<li class="money">
<h3>Deposited Amount</h3>
<p>#Model.Transaction.Amount.ToString() BTC</p>
</li>
<li class="time">
<h3>Time</h3>
<p>#Model.Transaction.Time.ToString()</p>
</li>
#using (Html.BeginForm("Send", "DepositDetails", FormMethod.Post, new { transaction = Model }))
{
#Html.HiddenFor(m => m.Token);
#Html.HiddenFor(m => m.Transaction.TransactionId);
#Html.TextBoxFor(m => m.WalletAddress, new { placeholder = "Wallet Address", maxlength = "34" })
<input type="submit" value="Send" />
#Html.ValidationMessage("walletAddress", new { #class = "validation" })
}
When i click on submit, the conroller contains the correct value of the walletAddress field but transaction.Transaction.Time, transaction.Transaction.Location, transaction.Transaction.TransactionId are empty.
Is there a way i could pass the entire Model back to the controller?
Edit:
When i dont even receive the walletAddress in the controller. Everything gets nulled!
When i remove this line alone: #Html.HiddenFor(m => m.Transaction.TransactionId);
it works and i get the Token property on the controller, but when i add it back, all the properties of the transaction object on the controller are NULL.
Here is the BitcoinTransactionViewModel:
public class BitcoinTransactionViewModel
{
public string Token { get; set; }
public string WalletAddress { get; set; }
public BitcoinTransaction Transaction { get; set; }
}
public class BitcoinTransaction
{
public int Id { get; set; }
public BitcoinTransactionStatusTypes Status { get; set; }
public int TransactionId { get; set; }
public decimal Amount { get; set; }
public DateTime Time { get; set; }
public string Location { get; set; }
}
Any ideas?
EDIT: I figured it out, its in the marked answer below...
OK, I've been working on something else and bumpend into the same issue all over again.
Only this time I figured out how to make it work!
Here's the answer for anyone who might be interested:
Apparently, there is a naming convention. Pay attention:
This doesn't work:
// Controller
[HttpPost]
public ActionResult Send(BitcoinTransactionViewModel transaction)
{
}
// View
#using (Html.BeginForm("Send", "DepositDetails", FormMethod.Post, new { transaction = Model }))
{
#Html.HiddenFor(m => m.Token);
#Html.HiddenFor(m => m.Transaction.TransactionId);
.
.
This works:
// Controller
[HttpPost]
public ActionResult Send(BitcoinTransactionViewModel **RedeemTransaction**)
{
}
// View
#using (Html.BeginForm("Send", "DepositDetails", FormMethod.Post, new { **RedeemTransaction** = Model }))
{
#Html.HiddenFor(m => m.Token);
#Html.HiddenFor(m => m.Transaction.TransactionId);
.
.
In other words - a naming convention error! There was a naming ambiguity between the Model.Transaction property and my transaction form field + controller parameter. Unvelievable.
If you're experiencing the same problems make sure that your controller parameter name is unique - try renaming it to MyTestParameter or something like this...
In addition, if you want to send form values to the controller, you'll need to include them as hidden fields, and you're good to go.
The signature of the Send method that the form is posting to has a parameter named transaction, which seems to be confusing the model binder. Change the name of the parameter to be something not matching the name of a property on your model:
[HttpPost]
public ActionResult Send(BitcoinTransactionViewModel model)
{
}
Also, remove the htmlAttributes parameter from your BeginForm call, since that's not doing anything useful. It becomes:
#using (Html.BeginForm("Send", "DepositDetails", FormMethod.Post))
Any data coming back from the client could have been tampered with, so you should only post back the unique ID of the transaction and then retrieve any additional information about it from your data source to perform further processing. You'll also want to verify here that the user posting the data has access to the specified transaction ID since that could've been tampered with as well.
This isn't MVC specific. The HTML form will only post values contained within form elements inside the form. Your example is neither inside the form or in a form element (such as hidden inputs). You have to do this since MVC doesn't rely on View State. Put hidden fields inside the form:
#Html.HiddenFor(x => x.Transaction.Time)
// etc...
Ask yourself though.. if the user isn't updating these values.. does your action method require them?
Model binding hydrates your view model in your controller action via posted form values. I don't see any form controls for your aforementioned variables, so nothing would get posted back. Can you see if you have any joy with this?
#using (Html.BeginForm("Send", "DepositDetails", FormMethod.Post, new { transaction = Model }))
{
#Html.TextBoxFor(m => m.WalletAddress, new { placeholder = "Wallet Address", maxlength = "34" })
#Html.Hidden("Time", Model.Transaction.Time)
#Html.Hidden("Location", Model.Transaction.Location)
#Html.Hidden("TransactionId", Model.Transaction.TransactionId)
<input type="submit" value="Send" />
#Html.ValidationMessage("walletAddress", new { #class = "validation" })
}
Try to loop with the folowing statement not with FOREACH
<table>
#for (var i = 0; i < Model.itemlist.Count; i++)
{
<tr>
<td>
#Html.HiddenFor(x => x.itemlist[i].Id)
#Html.HiddenFor(x => x.itemlist[i].Name)
#Html.DisplayFor(x => x.itemlist[i].Name)
</td>
</tr>
}
</table>
Try Form Collections and get the value as. I think this may work.
public ActionResult Send(FormCollection frm)
{
var time = frm['Transaction.Time'];
}
Put all fields inside the form
#using (Html.BeginForm("Send", "DepositDetails", FormMethod.Post))
and make sure that the model
BitcoinTransactionViewModel
included in view or not?
Can you just combine those 2 models you have? Here's how I do it with one model per view...
1. I use Display Templates from view to view so I can pass the whole model as well as leave data encrypted..
2. Setup your main view like this...
#model IEnumerable<LecExamRes.Models.SelectionModel.GroupModel>
<div id="container">
<div class="selectLabel">Select a Location:</div><br />
#foreach (var item in Model)
{
#Html.DisplayFor(model=>item)
}
</div>
3. Create a DisplayTemplates folder in shared. Create a view, naming it like your model your want to pass because a DisplayFor looks for the display template named after the model your using, I call mine GroupModel. Think of a display template as an object instance of your enumeration. Groupmodel Looks like this, I'm simply assigning a group to a button.
#model LecExamRes.Models.SelectionModel.GroupModel
#using LecExamRes.Helpers
#using (Html.BeginForm("Index", "Home", null, FormMethod.Post))
{
<div class="mlink">
#Html.AntiForgeryToken()
#Html.EncryptedHiddenFor(model => model.GroupKey)
#Html.EncryptedHiddenFor(model => model.GroupName)
<p>
<input type="submit" name="gbtn" class="groovybutton" value=" #Model.GroupKey ">
</p>
</div>
}
4. Here's the Controller.
*GET & POST *
public ActionResult Index()
{
// Create a new Patron object upon user's first visit to the page.
_patron = new Patron((WindowsIdentity)User.Identity);
Session["patron"] = _patron;
var lstGroups = new List<SelectionModel.GroupModel>();
var rMgr = new DataStoreManager.ResourceManager();
// GetResourceGroups will return an empty list if no resource groups where found.
var resGroups = rMgr.GetResourceGroups();
// Add the available resource groups to list.
foreach (var resource in resGroups)
{
var group = new SelectionModel.GroupModel();
rMgr.GetResourcesByGroup(resource.Key);
group.GroupName = resource.Value;
group.GroupKey = resource.Key;
lstGroups.Add(group);
}
return View(lstGroups);
}
[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult Index(SelectionModel.GroupModel item)
{
if (!ModelState.IsValid)
return View();
if (item.GroupKey != null && item.GroupName != null)
{
var rModel = new SelectionModel.ReserveModel
{
LocationKey = item.GroupKey,
Location = item.GroupName
};
Session["rModel"] = rModel;
}
//So now my date model will have Group info in session ready to use
return RedirectToAction("Date", "Home");
}
5. Now if I've got alot of Views with different models, I typically use a model related to the view and then a session obj that grabs data from each model so in the end I've got data to submit.
The action name to which the data will be posted should be same as the name of the action from which the data is being posted. The only difference should be that the second action where the data is bein posted should have [HttpPost] and the Posting method should serve only Get requests.

Form in Partial not binding to model

I've got a page basically displaying an article backed by a database.
Below that article, there is a comments section. This is provided by a #Html.Action call returning a _Comments partial.
Within that _Comments partial. There is an optional _AddComment #Html.Action call that renders a _AddComment partial within it.
The _AddComment partial is backed by _AddComment controller methods for GET and POST.
[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult _AddComment(EditComment comment)
The GET method just returns an "EditComment" VM with the AssetID attached.
Whenever a comment is filled in and posted within the _AddComment view. It's controller method is called correctly, but the model isn't passed back.
If I look at the Request parameters I can see all the properties of the model being passed back correctly. However, it's not being bound into the Controllers method parameter.
I've tried specifying "Model" as the route params for the Html.Begin form. It's made no difference.
Have looked at a number of SO posts, none of which sort the issue I'm having!
Presumably the model binding is failing somewhere for some reason. But obviously without an exception I've no idea what's wrong!
View Model Code
public class EditComment
{
public Boolean HasRating { get; set; }
public int AssetID { get; set; }
public int CommentID { get; set; }
public int Rating { get; set; }
public string Comment { get; set; }
}
View Code
#model SEISMatch.UI.Models.Content.EditComment
<hr />
<h3><span class="colored">///</span> Leave a Comment</h3>
<div class="row" style="margin-top: 20px;">
#using (Html.BeginForm("_AddComment", "Content", Model, FormMethod.Post))
{
#Html.ValidationSummary(false)
#Html.AntiForgeryToken()
#Html.HiddenFor(m => m.AssetID)
#Html.HiddenFor(m => m.CommentID)
if (Model.HasRating)
{
#Html.EditorFor(m => m.Rating, "_StarRating")
}
<div class="span7">
#Html.TextAreaFor(m => m.Comment, new { #class = "span7", placeholder = "Comment", rows = "5" })
</div>
<div class="span7 center">
<button type="submit" class="btn btn-success">Post comment</button>
</div>
}
</div>
Your action parameter name is comment and class EditComment has Property Comment. Modelbinder gets confused.
Rename your action parameter and problem solved.
[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult _AddComment(EditComment model)

How do I keep the clicked state of a checkbox in asp.net

I'm really having problems with keeping the state of my checkbox in my mvc4 application. I'm trying to send its value down to my controller logic, and refresh a list in my model based on the given value, before I send the model back up to the view with the new values. Given that my checkbox is a "show disabled elements in list" type function, I need it to be able to switch on and off. I've seen so many different solutions to this, but I can't seem to get them to work :(
Here's a part of my view:
#model MyProject.Models.HomeViewModel
<div class="row-fluid">
<div class="span12">
<div class="k-block">
<form action="~/Home/Index" name="refreshForm" method="POST">
<p>Include disabled units: #Html.CheckBoxFor(m => m.Refresh)</p>
<input type="submit" class="k-button" value="Refresh" />
#* KendoUI Grid code *#
</div>
</div>
HomeViewModel:
public class HomeViewModel
{
public List<UnitService.UnitType> UnitTypes { get; set; }
public bool Refresh { get; set; }
}
The HomeViewController will need some refactoring, but that will be a new task
[HttpPost]
public ActionResult Index(FormCollection formCollection, HomeViewModel model)
{
bool showDisabled = model.Refresh;
FilteredList = new List<UnitType>();
Model = new HomeViewModel();
var client = new UnitServiceClient();
var listOfUnitsFromService = client.GetListOfUnits(showDisabled);
if (!showDisabled)
{
FilteredList = listOfUnitsFromService.Where(unit => !unit.Disabled).ToList();
Model.UnitTypes = FilteredList;
return View(Model);
}
FilteredList = listOfUnitsFromService.ToList();
Model.UnitTypes = FilteredList;
return View(Model);
}
You return your Model to your view, so your Model properties will be populated, but your checkbox value is not part of your model! The solution is to do away with the FormCollection entirely and add the checkbox to your view model:
public class HomeViewModel
{
... // HomeViewModel's current properties go here
public bool Refresh { get; set; }
}
In your view:
#Html.CheckBoxFor(m => m.Refresh)
In your controller:
[HttpPost]
public ActionResult Index(HomeViewModel model)
{
/* Some logic here about model.Refresh */
return View(model);
}
As an aside, I can't see any reason why you'd want to add this value to the session as you do now (unless there's something that isn't evident in the code you've posted.

Categories