asp.net mvc Getting values from Html.Checkbox - c#

I want to be able to select a few boxes and send the strings correlating to them. The problem however is that I don't know how to catch the values in the controller.
One important issue is that I do not want to bind it to a model! Hopefully you can help me find a solution that fits in this image
cshtml file:
#model IEnumerable<WebApplication1.Models.WikiPage>
#using (Html.BeginForm("DeletePages", "Home", FormMethod.Post))
{
<ul>
#foreach (var item in #Model)
{
<li>
#Html.CheckBox(item.Title)
#Html.Label(item.Title)
</li>
}
</ul>
<input type="submit" value="Delete pages" />
}
And here is the part in the controller. String... will be null and I don't know what I should put in there to catch the values
[HttpPost]
public ActionResult DeletePages(String... Titles)
{
removePages(Titles);
List<WikiPage> pages = db.WikiPages.ToList();
return View(pages);
}
Hope you can help me!
Kind regards

View:
#model IEnumerable<WebApplication1.Models.WikiPage>
#using (Html.BeginForm("DeletePages", "Home", FormMethod.Post))
{
<ul>
#foreach (var item in Model)
{
<li>
<label>#item.Title
<input type="checkbox" name="titles" value="#item.Title />
</label>
</li>
}
</ul>
<input type="submit" value="Delete pages" />
}
Controller:
[HttpPost]
public ActionResult DeletePages(string[] titles)
{
removePages(titles);
List<WikiPage> pages = db.WikiPages.ToList();
return View(pages);
}

Related

attribute information not being found

When the button is pressed it states that the model is invalid as fields are empty.
when looking at the model all fields are empty or null. anyone know how to solve this issue, thanks
Home Controller
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> createOrderLine(Product models)
{
if (ModelState.IsValid)
{
db.OrderLines.Add(new OrderLine
{
productId = models.productId,
productColour = models.productColour,
productDescription = models.productDescription,
productName = models.productName,
productPrice = models.productPrice,
productStock = models.productStock,
//QuantityOrdered = quantityOrdered
});
db.SaveChanges();
return RedirectToAction("Index", "OrderLines");
}
else return RedirectToAction("Index", "Home");
}
Home Index - Displays the product information to the screen
#foreach (var product in Model)
{
using (Html.BeginForm("createOrderLine", "Home", FormMethod.Post, new {
#class = "form-horizontal", role = "form" }))
{
<div class="row">
<div class="blog col-md-6">
<div>
<h1>Product Name</h1>
<h2>#product.productName</h2>
<div>
<h3>Product Description</h3>
<h6><i>#product.productDescription</i></h6>
<br />
<h3>Product Price</h3>
<td>#product.productPrice </td>
<br />
<h1>Product colour</h1>
<td>#product.productColour</td>
<div></div>
<br />
<br />
#Html.AntiForgeryToken()
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-buy" value="Add to cart" onclick="btnbuy_Click" />
</div>
</div>
</div>
</div>
<br />
</div>
</div>
}
}
the problem is that none of your field are linked to the model they only display it so to fix this
you can simply add a hiddenfor field that links the product id in your page like so for each of your product :
#Html.HiddenFor(product.Id)
then fill the rest of the informations you need with the id that is gonna be passed to your controller as a parameter
exemple :
public ActionResult createorderline(Product ProductToAdd)
{
// add your product or do your treatment
}
I do not quite understand what are you trying to do, but Louis is right. If you are trying to pass values back, you have to #Html.EditorFor() or some other ways to pass back information to the server.
You could also use #Html.HiddenFor(product.Id) then inside your Actionmethod call the database to get the rest of the info regarding your product or you could use JQuery as well.
In this case I would only use the #Html.HiddenFor(product.Id) and have a static helper method that will map your orderline class from the DB.
public static Product GetProductById(int productId){
return db.Products.FirstOrDefault(x => x.Id == productId);
}
But again you could just add the FirstOrDefault() line inside your controller as you not mapping from a ViewModel to a business Model.

Inserting list from view to Model in MVC c#

I have been returning a list from my controller to the view;
public ActionResult Raffle_Conso(int conso = 0)
{
return View(db.Users_Info.Where(s=>s.by_name_rank == null).OrderBy(t => Guid.NewGuid()).Take(conso));
}
In my view, I looped it and put in an input field.
#using (Html.BeginForm("InsertRafflers","Events", FormMethod.Post))
{
#foreach (var item in Model)
{
<input type="text" name="rafflers_name" value="#item.rafflers_name" />
}
<input type="submit" value="Insert list" />}
}
It displayed the list. I like to ask if how would it be possible to insert the list from the view to the database via controller?
Any idea is really appreciated. Thanks.
Your control name needs to be named like an array...
#using (Html.BeginForm("InsertRafflers","Events", FormMethod.Post))
{
var textIndex = 0;
#foreach (var item in Model)
{
#<input type="text" name="rafflers_name[#textIndex]" value="#item.rafflers_name" />
textIndex += 1;
}
<input type="submit" value="Insert list" />}
}

asp.net mvc 4 [HttpPost] not working

I am new to asp.net MVC 4. i have some problems dealing with attributs
i use [httppost] attribut in my controller but it's not working
it's not even invoked
my controller
public ActionResult Inscription()
{
return View();
}
[HttpPost]
public ActionResult Inscription(Candidat candidat)
{
if (!ModelState.IsValid)
{
return View(candidat);
}
return RedirectToAction("Index");
}
my view
#model ProcRec.Models.Candidat
#{
ViewBag.Title = "Inscription";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#Html.ValidationSummary(true)
<div class="form_settings">
#using (Html.BeginForm("Inscription", "CandidatController"))
{
<table ="#FFFFFF">
<tr>
<td>#*<span >Nom :</span>*# #Html.LabelFor(model => model.nom_candidat)</td>
<td> #Html.TextBoxFor(model => model.nom_candidat)
#Html.ValidationMessageFor(Model => Model.nom_candidat)
.
.
</table>
}
<input type="submit" class="submit right" value="Inscription" />
think you for your help
Just correct beginform as :
#using (Html.BeginForm("Inscription", "CandidatController",FormMethod.Post))
{
........
<input type="submit" class="submit right" value="Inscription" />
}
Put submit button inside BeginForm() and give BeginForm() FormMethod as Post.
Thankzz..
Put your <submit> element inside the form:
#using (Html.BeginForm())
{
...
<input type="submit" class="submit right" value="Inscription" />
}
Don't need to change Html.BeginForm. By default it's POST.
Check : FormExtensions.BeginForm
Only issue is with the submit button, it should be inside form as mentioned in Andy Refuerzo's answer. Don't know why it is down-voted.

How to display details about specific record on a same page in ASP.net MVC

I have two divs on my view, the left one contains all of the products available. I want to display the details of the product selected on the same page in the right div. When I first load the page, the right div should contain the first record's details. After that, when I click on any of the links, it should change the details. I'm new to MVC and Entity Framework. I have tried to do it but have no idea. If I want to display the details in the next page after clicking on the hyperlink of the product, I can do that. Please try to solve my problem. Thanks!
Products.cshtml:
#model List<productList.Models.tbl_product>
<div class="product">
<ul class="list">
#foreach(var item in Model)
{
<li>
<a href='#Url.Action("Product","Home",new {id = item.ProductId })'>
<span class="ProductName">#item.ProductName</span>
</a>
</li>
</span></a>
</div>
<div class="productDetails">
<ul class="listdetails">
#foreach(var item in Model)
{
<li>
<span class="ProductName">#item.ProductName</span>
<div class="ProductDesc">
#item.ProductDetails
</div>
</li>
}
HomeController.cs:
public ActionResult Product()
{
return View(dbentity.tbl_product.ToList());
}
Use RenderPartial for the first product load, after that use jQuery ajax to load other
Controller action for first product list load
public ActionResult Product()
{
return View(dbentity.tbl_product.ToList());
}
PartialView.cshtml
#model Product
<label>Name:</label>#model.Name
In Main View
#model IList<Product>
<div class="product">
<ul class="list">
#foreach(var item in Model)
{
<li> <a href=#Url.Action("ProductDetail","Home",new {id=item.ProductId})'
<span class="ProductName"> #item.ProductName</span></a></li>
}
</ul>
</div>
<div id="product-detail">
#{Html.RenderPartial("PartialView", Model.FirstOrDefault());}
</div>
Also you need another action for loading each product
public ActionResult ProductDetail(int id)
{
var model=products.Find(id);
return ("PartialView",model);
}
AJAX CALL
$("div.product a").click(function(e){
var url=this.href;
$.get(url,{},function(data){
$('#product-detail').html(data);
});
e.preventDefault();
});

ASP.NET MVC Redirect

I work on one of my first ASP MVC-programs at the moment.
The program should show me a list of product, and with a link beneath the name of the product it should be possible to edit the product. No problem so far.
#model MVC3Demo.Product
#{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
#using (Html.BeginForm("Save", "Product"))
{
<div>
<input type="hidden" id="ID" name="ID" value="#Model.ID" />
ProduktID #Model.ID
</div>
<div>
Produktname <input id="Name" name="Name" type="text" value=#Model.Name />
</div>
<div>
Preis <input id="Price" name="Price" type="text" value=#Model.Price />
</div>
<div>
<input type="submit" value="Speichern"/>
</div>
}
Now I have written a Save action method that should update my data:
public ActionResult Save(Product p)
{
ProductRepository rep = new ProductRepository();
rep.Update(p);
return RedirectToAction("List");
}
The "List"-View is where I can see all products with the edit-link.
The problem is, that if I press the save-button, it redirects me to the old list, not to the updated one. I debugged my project and I´m sure that the update-method works correct and updates the product.
My List action is:
#model IEnumerable<MVC3Demo.Product>
#{
ViewBag.Title = "List";
}
<h2>List</h2>
<ul>
#foreach (MVC3Demo.Product p in Model)
{
<li>#p.Name #Html.ActionLink("bearbeiten", "Edit", "Product", p, null)</li> //new{ ID = p.id}
}
</ul>
Because you asked, here is the List Action:
public ActionResult List()
{
ProductRepository rep = new ProductRepository();
return View(rep.GetAll());
}
So where could be my mistake?
It looks like you're calling the update, but you're not actually submitting the transaction itself, does your repository have a SubmitChanges, AcceptChanges or Commit or something similar? As with DataTables, your changes won't actually take effect (save to the database) until you call AcceptChanges.
Try include an HttpPost attribute at Save controller method.
[HttpPost]
public ActionResult Save(Product p)
{
ProductRepository rep = new ProductRepository();
rep.Update(p);
return RedirectToAction("List");
}

Categories

Resources