MVC Html.HiddenFor in a loop passing model back to Controller - c#

I am looping through an IEnumerable of my model:
#model IEnumerable<Testing.Models.ProductItem>
#{
ViewBag.Title = "Buy Products";
}
<div class="row">
#foreach (var product in Model)
{
using (Html.BeginForm())
{
#Html.HiddenFor(Model => product)
... More Controls and stuff...
<input type="submit" value="Add To Kart" class="btn btn-info">
}
}
</div>
and on submit I want to pass the selected instance of my model back to my controller:
[HttpPost]
public ActionResult Index(ProductItem product)
{
... Do Stuff ...
return View();
}
However I have tried a few things but always seem to be getting null passed into the controller... Please could someone please help point me in the right direction?
EDIT
I dont actually need to the full model instance as I can get this within the controller from the ID - so I have tried the following:
#model IEnumerable<Testing.Models.ProductItem>
#{
ViewBag.Title = "Buy Products";
}
<div class="row">
#foreach (var product in Model)
{
using (Html.BeginForm())
{
#Html.HiddenFor(Model => product.ID)
#Html.TextBox("qty", "1", htmlAttributes: new { #style = "width: 30px;" })
... More Controls and stuff...
<input type="submit" value="Add To Kart" class="btn btn-info">
}
}
</div>
which posts to the controller:
[HttpPost]
public ActionResult Index([Bind(Include = "ID")] int? ID, [Bind(Include = "qty")] int? qty)
{
return null;
}
The textbox is not part of the model as it is user input - this value is passed nicely into the actions parameter, however I am still getting a null for the ID in the HiddenFor control. Is this to do with the naming of the control? I dont seem to be able to add a name to the HiddenFor control.
I know this puts a different light on the original question but I am hoping you may still be able to help.
I take the note about the BeginForm being inside the loop - creating for each item in the list... Is there an easy alternative to this (note I haven't tried anything yet).

It sounds like you're trying to use HiddenFor on a complex type and that won't work. You'll need to use a property of ProductItem like ProductId or something like that, which will most likely be an int or Guid.
Now that you have cleared up the complex binding to a simple field, you'll notice that your name is being set to product.id and that is why it is always null in your controller. You can't override the name attribute with Hidden for, so you'll want to change your code to:
#foreach (var product in Model)
{
using (Html.BeginForm())
{
#Html.Hidden("ID", product.ID)
#Html.TextBox("qty", "1", htmlAttributes: new { #style = "width: 30px;" })
<input type="submit" value = "Add To Kart" class="btn btn-info">
}
}

I have managed to arrive at my desired functionality (rightly or wrongly) with the following:
#model List<ShoppingKartTest.Models.ProductItem>
#{
ViewBag.Title = "Buy Products";
}
#foreach (var item in Model)
{
using (Html.BeginForm())
{
<input type="hidden" value="#item.ID" name="ID" />
#Html.TextBox("qty", "1", new { #style = "width: 30px;" })
<input type="submit" value="Add To Kart" class="btn btn-info">
}
}
Which correctly submits the Hidden ID and the contents of the Textbox to the Controller Action:
[HttpPost]
public ActionResult Index(int ID, int qty)
{
//... Do stuff with parameters...
return View();
}
I would be interested to hear any comments on this. I know that I was told above that I shouldn't have my BeginForm within the loop... But it just works for me.

Instead of Model => product.Id, try p=> product.Id
#model IEnumerable<Testing.Models.ProductItem>
#{
ViewBag.Title = "Buy Products";
}
<div class="row">
using (Html.BeginForm())
{
#foreach (var product in Model)
{
#Html.HiddenFor(p => product.ID)
#Html.TextBox("qty", "1", htmlAttributes: new { #style = "width:
30px;" })
... More Controls and stuff...
}
<input type="submit" value="Add To Kart" class="btn btn-info">
}
</div>

Related

Access 2 records at the same time?

I would like to know if there is a way to access 2 rows of a model table in my view for Edit. As I would like to edit one of the record only in the view, I cannot load a list that contains the 2 methods at the GET edit controller method. Is there any way that allows my to query for the next record inside my view?
My controller methods
// GET: Record/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Record record = db.Records.Find(id);
if (record == null)
{
return HttpNotFound();
}
return View(record);
}
// POST: Record/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "RecordID,BeforeDate,AfterDate,ShiftIDBefore,ShiftIDAfter,EmpAID,EmpBID,Reason,Self_interchange,Status")] Record record, string button)
{
//Codes for modifying the record
}
My view
#model Interchangesys.Models.Record
#{
ViewBag.Title = "Edit";
}
<h2>Process interchange record</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.RecordID)
<dt>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" name="button" value="Accept" class="btn btn-success btn-lg" style="width:200px;height:50px;font-size: 25px;" />
<input type="submit" name="button" value="Reject" class="btn btn-danger btn-lg" style="width:200px;height:50px;font-size: 25px;" />
</div>
</div>
</dt>
<!--rest of the codes that shows the content of record with the same id and content of record with the id +1-->
}
Thanks in advance.
Well, if I understand you correctly, you just want to show info about two records in your view.
You could pass the the next record to be available in your View via ViewBag like this:
Record record = db.Records.Find(id);
Record next = db.Records.Find(id + 1);
ViewBag.Next = next;
return(record);
And then in your view you access it like:
#{
Record next = (Record)ViewBag.Next;
}
And finally, to use it in a view, you would do something like this:
#Html.Hidden("NextRecordID", next.RecordID) // You can't use #Html.HiddenFor
Alternatively, you could pass both records via ViewBag, like this:
Record record = db.Records.Find(id);
Record next = db.Records.Find(id + 1);
ViewBag.Record = record
ViewBag.Next = next;
return(); // There will be no model for your view.
And then prefix the fields in the view like this:
#{
Record record = (Record)ViewBag.Record;
Record next = (Record)ViewBag.Next;
}
#Html.Hidden("record.RecordID", record.RecordID)
#Html.Hidden("next.RecordID", next.RecordID)
So you get them both in your Edit function like this:
public ActionResult Edit([Bind(Prefix = "record", Include = "...")] Record record, [Bind(Prefix = "next", Include = "...")] Record next, string button)

Pass value of dropdownlist when click button

I have a dropdownlist in my View:
#Html.DropDownListFor(m => m.APtTitleData.apt, (IEnumerable<SelectListItem>)ViewBag.apt, "-Select Apt-", new { id = "SelectedAPt", name= "SelectedAPt" })
and I have a button in the same View
GO
How do I pass the value of the dropdown to my controller (Edit)? I'm trying to get the value to the button, but I'm not sure this is the right way. Any other idea?
I would suggest that you use a form where you submit the value from the dropdown to the controller.
Please check out the following code:
#using (Html.BeginForm("APtTitle", "Edit", FormMethod.Post))
{
<div class="form-group">
<label>AptTitles</label>
<div>
#Html.DropDownListFor(m => m.APtTitleData.apt, (IEnumerable<SelectListItem>)ViewBag.apt, "-Select Apt-", new { id = "SelectedAPt", name= "SelectedAPt" })
</div>
</div>
<div class="form-group">
<div>
<input type="submit" value="Submit" />
</div>
</div>
</div>
}
And in the controller your code should look similar to the following:
[HttpPost]
public ActionResult Edit(string SelectedAPt)
{
var aPtValue = SelectedAPt;
// Do what intended with the value
}
You should now be able to see the value in the controller now.
Try it out and let me know if you run into issues.

#Html.DropDownList return null to controller

The DropDownList in my view shows the relevant options to choose, but no matter what i choose, the folders in the Controller get value null.
Why? How can i fix it so the folders in the Controller will get the chosen option from the DropDownList from the view?
P.S - I have no Model.
This is my Controller:
//POST: Home
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(IEnumerable<HttpPostedFileBase> file, string folder, IEnumerable<SelectListItem> folders)
{
// some code here
}
This is my view:
#using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.AntiForgeryToken();
<div class="container">
<div class="form-horizontal">
<div class="form-group">
<p></p>
<label for="file">Upload Photo:</label>
<input type="file" name="file" id="file" accept="image/*" multiple="multiple"/>
</div>
<div class="form-group">
<div>
<label>Choose Album:</label>
#if (ViewBag.Folders != null)
{
#Html.DropDownList("folders", new SelectList(ViewBag.Folders as IEnumerable<SelectListItem>, "Value", "Text"), "--- Select Album ---", new { #class = "form-control" })
}
</div>
</div>
<div class="form-group">
<div>
<input type="submit" value="Upload" class="btn btn-default" />
</div>
</div>
</div>
</div>
}
Thanks.
in order to get the folder, give your dropdownlist the corresponding name ...
#Html.DropDownList("folders"...
will result in your DDL to have the name "folders" ... which will try to post back a single item... folders in your method is a IEnumerable<SelectListItem> ... the modelbinder is incapable to convert that ...
try
#Html.DropDownList("folder"...
note the missing s
now the name corresponds to the string folder parameter in your method... which the binder will most likely be able to bind for you...
if you debug errors like this, use the debugger to have a look at HttpContext.Request.Params, which will show you what was coming back when the request was made...
Parameter type should be changed from IEnumerable to String as view returns only selected item NOT collection.
//POST: Home
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(IEnumerable<HttpPostedFileBase> file, string folder, string folders)
{
// some code here
}
Just wanted to give another option. If you're able to leverage a client-side solution, then you could muscle this a bit with a hidden input value and JQuery.
Add a hidden input control:
<input name="selectedFolder" type="hidden" value="" />
Add some Jquery:
<script type="text/javascript">
$(function(){
$("#folders").on("change", function {
$("#selectedFolder").val($(this).text());
});
});
</script>
Thank you Sakthivel Ganesan, after changing the IEnumerable<SelectListItem> folders in the Controller to string folders, the only thing left to do is to change my Html.DropDownList Value to Text like this...
from :
#Html.DropDownList("folders", new SelectList(ViewBag.Folders, "Value", "Text"), "--- Select Album ---", new { #class = "form-control" })
To:
#Html.DropDownList("folders", new SelectList(ViewBag.Folders, "Text", "Text"), "--- Select Album ---", new { #class = "form-control" })

MVC 5 Viewmodel binding works but post back is partial filled

I have a parameterless Index for the HttpGet which works. But when I post it the HttpPost version of Index is invoked and the viewmodel object is passed in, but there is only the value of the dropdown in it. The rest is null (products, title)
[HttpPost]
public ActionResult Index(ProductsViewModel pvm)
{
// breakpoint on line 36, shows that pvm.Title is null and Products too.
return View(pvm);
}
My compilable and running example can be downloaded from my OneDrive http://1drv.ms/1zSsMkr
My view:
#model KleinKloteProductOverzicht.Models.ProductsViewModel
#using (Html.BeginForm("Index", "Products"))
{
<h2>#Html.DisplayFor(m => m.Title)</h2>
<input type="submit" value="post dit" /><br/>
<div class="row">
<div class="col-lg-2 col-md-2">
#Html.DropDownListFor(x => x.CurrentSort, EnumHelper.GetSelectList(typeof(SortOptions)), new { #class = "multiselect"})
</div>
</div>
if (Model.Products.Count() > 0)
{
<div class="row">
#foreach (var item in Model.Products)
{
#Html.DisplayFor(i => item.Name);
}
</div>
}
}
If I have this view model:
public class ViewModel
{
public string Name {get;set;}
public string SelectedLocation {get;set;}
public IEnumerable<SelectListItem> Locations {get;set;}
}
And your actions look like this:
public ActionResult MyForm()
{
var vm = new ViewModel
{
Locations = context.Locations.ToList() // Some database call
}
return View(vm);
}
[HttpPost]
public ActionResult MyForm(ViewModel vm)
{
vm.Locations // this is null
}
It is null because the model binder can't find a form control that is setting its data.
The <form> must set some data in the view for the model binder to pick it up.
<form>
Name: <input type="text" id="name" />
</form>
This will set the Name property on the view model, because the model bind can see the id of the form control and uses that to know what to bind to.
So in terms of your view, you need to make sure you wrap any content that you want to post back to the server with #using(Html.BeginForm())
Anyway this is my guess.
Well, you seem to be confused as to how [HttpPost] and form tags interact with eachother.
You see, when .NET MVC binds your parameters in your controller actions, it tries to derive that data from the request. For [HttpGet] it does this by looking at the query string.
For [HttpPost] calls, it also looks at the Request.Form. This variable is populated with the values of all input fields that were inside the form you submitted.
Now, this is your view:
#using (Html.BeginForm("Index", "Products"))
{
<h2>#Html.DisplayFor(m => m.Title)</h2>
<input type="submit" value="post dit" /><br/>
<div class="row">
<div class="col-lg-2 col-md-2">
#Html.DropDownListFor(x => x.CurrentSort, EnumHelper.GetSelectList(typeof(SortOptions)), new { #class = "multiselect" })
</div>
</div>
if (Model.Products.Count() > 0)
{
<div class="row">
#foreach (var item in Model.Products)
{
#Html.DisplayFor(i => item.Name);
}
</div>
}
}
You only have one select tag (generated by Dropdownlistfor) but no other inputs. That's why .NET MVC cannot infer any other data for your view model.
If you change your view to this:
#model KleinKloteProductOverzicht.Models.ProductsViewModel
#using (Html.BeginForm("Index", "Products"))
{
<h2>#Html.DisplayFor(m => m.Title)</h2>
<input type="submit" value="post dit" /><br/>
<div class="row">
<div class="col-lg-2 col-md-2">
#Html.DropDownListFor(x => x.CurrentSort, EnumHelper.GetSelectList(typeof(SortOptions)), new { #class = "multiselect" })
</div>
</div>
if (Model.Products.Count() > 0)
{
<div class="row">
#for (var i = 0; i < Model.Products.Count; i++)
{
#Html.DisplayFor(model => model.Products[i].Name)
#Html.HiddenFor(model => model.Products[i].ID)
}
</div>
}
}
You'll see I've added a hidden input (<input type="hidden">) for the product id. Note that the product name still will be null.
I would suggest you follow a tutorial on .NET MVC and read up on some of the concepts behind it, because the very fact that you ask this question reveals that you have much to learn.
Best of luck!
P.S. One last tip: #Html.Blablabla writes directly to your view. You usually don't need that ";" at the end, because it will be inside your generated html.
Your property is not associated with a "postable" control, therefore it will not be submitted along with the form data. If your really want to get the value in your Title property, just set it as a hidden input.
#Html.HiddenFor(m => m.Title)
A label will not be posted when submitting a form but an input will. This is exactly what HiddenFor does; it creates a hidden input element which will be picked up by the form submit.

post values through mvc repeater

Form-
#using IEnumerable<Myapplication.Models.CardModel>
#foreach(var item in Model)
{
<form method="post" action="/Upload/EditCard/?cardID=#item.cardID" enctype="multipart/form-data">
<h3>
Change Title-
</h3>
<div class="display-field">
#Html.HiddenFor(m => item.cardTitle)
#Html.TextBoxFor(cardTitle => item.cardTitle)
</div>
<img src="#item.cardFilePath" />
<input type="submit">
</form>
}
Method-
[HttpPost]
public void EditCard(CardModel card, HttpPostedFileBase file) {}
Where in form I am sending values through this form, and cardID is sent in form's url parameter.
For other value like cardTitle is coming null in EditCard Method.
How do I get this value using repeater?
However when data is not IEnumerable type , then I was able to send value through form directly as-
#using Myapplication.Models.CardModel
<form method="post" action="/Upload/EditCard/?cardID=#Model.cardID" enctype="multipart/form-data">
<h3>
Change Title-
</h3>
<div class="display-field">
#Html.HiddenFor(m => m.cardTitle)
#Html.TextBoxFor(m=> m.cardTitle)
</div>
<img src="#Model.cardFilePath" />
<input type="submit">
</form>
}
but In case of repeater values don't come at method. As soon as i change values of title, or anything else, The values comes old one.
From the pictures-
And server code-
As you can see from 1st picture that, Form is editable for one record from the list. As soon as I change something in i.e. Title to --> 2nd Upload to 2nd Uploadsssss
Then this values is null at server side. I mean this form don't send values of it.
Note-
However I can send values in URL through parameters. But if I do change in something like Title or aboutCard model value. The form keeps sending only those values which has come by default while the form was rendered.
Rather than using foreach use for loop. To apply indexing you would need to convert Model to List
#{ var list=Model.ToList();)
#for(var i = 1;i <= list.Count();i++)
{
<form method="post" action="/Upload/EditCard/?cardID=#list[i].cardID" enctype="multipart/form-data">
<h3>
Change Title-
</h3>
<div class="display-field">
#Html.HiddenFor(m => list[i].cardTitle)
#Html.TextBoxFor(m=> list[i].cardTitle)
</div>
<img src="#list[i].cardFilePath" />
<input type="submit">
</form>
}
Update..
I have tested same but doesnt work. I have created workaround check if that helps.
In controller get title using cardId
var title=Request["[" + (model.cardTitle)+ "].cardTitle"];
Same thing can be done for other properties for model.
Note that i have changed for loop, index now starts with 1
You need to change IEnumerable to IList, and then you'll be able to bind Model correctly by index.
See this working exaple.
View Page
#model IList<MvcApplication3.Models.CardModel>
#using (Html.BeginForm("EditCard", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
for (int i = 0; i < Model.Count; i++)
{
#Html.HiddenFor(m => Model[i].CardID)
#Html.TextBoxFor(cardTitle => Model[i].CardTitle)
<img src="#Model[i].CardFilePath" />
<input type="file" name="files">
}
<br>
<input type="submit" value="Upload File to Server">
}
Controller
[HttpPost]
public void EditCard(IList<CardModel> cm, IEnumerable<HttpPostedFileBase> files)
{
string strfile = string.Empty;
string cardTitle = string.Empty;
if (files != null)
{
foreach (var file in files) //loop through to get posted file
{
strfile = file.FileName;
}
}
if (cm != null)
{
foreach (var item in cm) //loop through to get CardModel fields
{
cardTitle = item.CardTitle;
}
}
}
I dont know if this helps in solving your problem. I tried this and it works really well.
The Model
public class CardFileModel
{
public int CardId { get; set; }
public string Name { get; set; }
public HttpPostedFileBase File { get; set; }
}
Index Action
public ActionResult Index()
{
List<CardFileModel> cards = new List<CardFileModel>
{
new CardFileModel{ CardId =1 , Name="Card1" },
new CardFileModel{ CardId =2 , Name="Card2" }
};
return View(cards);
}
Home View
#model List<MVC3Stack.Models.CardFileModel>
#{
ViewBag.Title = "Home Page";
}
<h2>#ViewBag.Message</h2>
#for (int i = 0; i < Model.Count; i++)
{
using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
<div>
#Html.TextBoxFor(x => x[i].CardId)
</div>
<div>
#Html.TextBoxFor(x => x[i].Name)
</div>
#Html.TextBoxFor(x => x[i].File, new { type = "file" })
<input type="submit" value="submit" id="submit" />
</div>
}
}
index post action
[HttpPost]
public ActionResult Index(List<CardFileModel> card)
{
//Access your card file from the list.
var name = card.FirstOrDefault().Name;
return View();
}
And here are the results in the quick watch
Hope this helps.

Categories