I'm inserting data from one (model)table to another, but it is passing null values.
Here is the code for the controller :
public ActionResult PendingBlogs()
{
OMSDataContext db = new OMSDataContext();
var query = from a in db.BlogApprovals
select a;
return View(query);
}
[HttpPost]
public ActionResult PendingBlogs(BlogApproval blogap)
{
OMSDataContext db = new OMSDataContext();
Blog b = new Blog
{
BlogTitle = blogap.BlogTitle1,
BlogContent = blogap.BlogContent1,
UserName = blogap.UserName1,
Date = blogap.Date1,
IsApproved = true
};
db.Blogs.InsertOnSubmit(b);
db.SubmitChanges();
return RedirectToAction("Index");
}
And here is my View code :
#model IEnumerable<MVCDemo.Models.BlogApproval>
#{
ViewBag.Title = "PendingBlogs";
}
<h2>PendingBlogs</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.BlogTitle1)
</th>
<th>
#Html.DisplayNameFor(model => model.BlogContent1)
</th>
<th>
#Html.DisplayNameFor(model => model.UserName1)
</th>
<th>
#Html.DisplayNameFor(model => model.Date1)
</th>
<th>
#Html.DisplayNameFor(model => model.IsApproved1)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.BlogTitle1)
</td>
<td>
#Html.DisplayFor(modelItem => item.BlogContent1)
</td>
<td>
#Html.DisplayFor(modelItem => item.UserName1)
</td>
<td>
#Html.DisplayFor(modelItem => item.Date1)
</td>
<td>
#Html.DisplayFor(modelItem => item.IsApproved1)
</td>
<td>
<form asp-controller="Admin" method="post">
<input type="submit" name="answer" value="RunRegisterdJob" />
</form>
</td>
</tr>
}
</table>
The two models (Blog and BlogApproval) are tables in the models folder. BlogApproval has data inside it, but it's not returning the data from the table for some reason.
I've looked at this thread, and have renamed the columns in the BlogApproval table so that they are different from the columns on the Blog table, but that didn't fix it.
Your form tag does not contain any html input value ,so instead of using below code
<form asp-controller="Admin" method="post">
<input type="submit" name="answer" value="RunRegisterdJob" />
</form>
use below code:
<form asp-controller="Admin" method="post">
<input type="text" name="blogTitle1"/>
<input type="text" name="blogContent"/>
<input type="text" name="UserName1"/>
<input type="submit" name="answer" value="RunRegisterdJob" />
</form>
Related
example of view priority list of orders
i have a orders model, i need the user to arrange orders in order of priority by numbers.
for each order a different number of priority and post to the controller the id of the order and the priority number that he selected for the order.
post it to the controller as a list/array of pairs (id, position).
and also how to receive in the actionResult a list/array of value pairs.
this is my PackerController:
public ActionResult SetByCity(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var supplier = db.Suppliers.Where(s => s.Id == id).FirstOrDefault();
var mySupplierOrders = db.Orders.Where(o => o.SupplierId == supplier.Id && o.SupplierApproval == 1).Include(o => o.Clients).Include(o => o.Suppliers);
return View(mySupplierOrders.OrderBy(o => o.Clients.BusinessAddress).ToList());
}
and this is the view for "SetByCity":
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Id)
</td>
<td>
#Html.DisplayFor(modelItem => item.Clients.BusinessName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Clients.BusinessAddress)
</td>
<td>
#Html.DisplayFor(modelItem => item.CreateDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.PayDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.Discount)
</td>
<td>
#Html.DisplayFor(modelItem => item.TotalPrice)
</td>
<td>
<form method="post" action="#Url.Action("SetOrdersPosition", "Packer")" id="editform">
<input type="hidden" name="Id" value="#item.Id">
<input type="number" name="Position" min="1" max="#Model.Count()">#*i need to put all the positions and id's of all the items in a pairs list and send it to the controller*#
</form>
</td>
</tr>
}
</table>
<input type="submit" value="send" form="editform" />
and this is the receiving actionResult "SetOrdersPosition" in the PackerController:
[HttpPost]
public ActionResult SetOrdersPosition(List<id,position>)
{
//does something...
}
i don't know what to put in the parameters that the SetOrdersPosition gets...
Hope you are using the Model name called "Suppliers".
So in get method pass that into view.
You needs to do come little changes accordingly to use Begin Form in your view, so that you can directly post your model data to controller.
Use this item inside body.
Don't forget to declare the model which you are using in the view page like below.
#model MVC.Models.Suppliers
#using (Html.BeginForm("SetOrdersPosition", "Packer", FormMethod.Post))
{
<table>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Id)
</td>
<td>
#Html.DisplayFor(modelItem => item.Clients.BusinessName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Clients.BusinessAddress)
</td>
<td>
#Html.DisplayFor(modelItem => item.CreateDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.PayDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.Discount)
</td>
<td>
#Html.DisplayFor(modelItem => item.TotalPrice)
</td>
</tr>
<tr>
<td><input type="submit" value="Submit"/></td>
</tr>
}
</table>
}
Controller Code [HttpPost]
public ActionResult SetOrdersPosition(Suppliers _suppliers)
{
//does something...
}
I am trying to send 3 values from a view to another action, using actionlink. I have a table . Each row has 3 columns , with id, name and bid money which is a textbox. The problem is the action link only passing the value of the textbox of the first row, suppose if I press the actionlink of the second row, it is passing, the name and id of the second row but the bid money which is in textbox, is passing of the first row.
my view code:
#model List<UdemyMVC.Models.Movies>
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr class="danger">
<th>
Id
</th>
<th>
Name
</th>
<th>
Bid Money
</th>
</tr>
</thead>
<tbody>
#foreach (var movie in Model)
{
<tr>
<td>
#movie.Id
</td>
<td>
#movie.MovieName
</td>
<td>
<input type="text" id="bidMoney" name="bidMoney"/>
</td>
<td>
#Html.ActionLink("Details", "Details", "Test", new { Id =movie.Id, MovieName = movie.MovieName }, new { onclick = "this.href += '&biddingMoney=' + document.getElementById('bidMoney').value;", #class = "btn btn-primary" })
</td>
</tr>
}
</tbody>
</table>
I think you should add an index to the id of bidMony and use that index as a reference to the one you want. My razor is totally rusty right now
but declare an index
#{ int i = 0}
#foreach (var movie in Model)
{
<tr>
<td>
#movie.Id
</td>
<td>
#movie.MovieName
</td>
<td>
<input type="text" id="bidMoney#(i)" name="bidMoney" />
</td>
<td>
#Html.ActionLink("Details", "Details", "Test", new { Id =movie.Id, MovieName = movie.MovieName }, new { onclick = string.Format("alert(document.getElementById('bidMoney{0}').value);",i), #class = "btn btn-primary" })
</td>
</tr>
i++;
}
I'm unable to test this but I think this is what you need to do. The problem is that you're getelementbyid see's a bunch of bidMoney elements but can only return one so it gives you the first one.
I have a Form which is filled with some grid like structure with CheckBoxes and DisplayField.
I want to fetch rows with Checked CheckBoxes. Problem is i am getting null in Controller's post method.
Models
public class RegisterModuleSelection
{
[Display(Name = "ID")]
public int mID { get; set; }
[Display(Name = "Module")]
public string Module { get; set; }
[Display(Name = "Buy")]
public bool Buy { get; set; }
}
View
#model IEnumerable<MAK_ERP.Models.RegisterModuleSelection>
#{
ViewBag.Title = "Register - Modules Selection";}
<h2>
Register - Modules Selection</h2>
#using (Html.BeginForm("RegisterModules", "UserAccount", FormMethod.Post, new { id = "RegisterModules", #class = "generalForm" }))
{
<table class="Grid Module">
<tr>
<th>
#Html.DisplayNameFor(model => model.Module)
</th>
<th>
#Html.DisplayNameFor(model => model.Price)
</th>
<th>
#Html.DisplayNameFor(model => model.Duration) (Months)
</th>
<th>
#Html.DisplayNameFor(model => model.Buy)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.HiddenFor(modelItem => item.mID)
#Html.DisplayFor(modelItem => item.Module)
</td>
<td>
#Html.DisplayFor(modelItem => item.Price)
</td>
<td>
#Html.EditorFor(modelItem => item.Duration)
</td>
<td>
#Html.CheckBoxFor(modelItem => item.Buy)
</td>
</tr>
}
<tr>
<td colspan="4">
<input type="submit" value="Next" class="button3" />
<input type="reset" value="Reset" class="button4" />
</td>
</tr>
</table>
}
Controller
[HttpGet]
public ActionResult RegisterModules()
{
IEnumerable<MAK_ERP.Models.RegisterModuleSelection> model = reg.getModules();
return View(model);
}
[HttpPost]
public ActionResult RegisterModules(IEnumerable<Models.RegisterModuleSelection> regMod)
{
//regMod is null here
...
Unfortunately you cannot bind in this way. Model binding depends upon how generated html looks like. In this particular case it should look something like:
<input id="item_Buy" type="checkbox" value="true" name="item[0].Buy" checked="checked">
If you are okay with some workarounds, you can use a for loop instead of foreach loop where you can add an index to each control name and model binder could bind them properly.
There are some really helpful discussions available, you can check here: ASP.NET MVC - Insert or Update view with IEnumerable model. And also Model Binding To A List. Another one is: Modelbinding IEnumerable in ASP.NET MVC POST?
You should use EditorTemplates to solve the problem.
The accepted answer at ASP.NET MVC Multiple Checkboxes would help you.
My question is how to get table data back to the controller from view?
I have class in my model:
public class Company
{
public string Name { get; set; }
public int ID { get; set; }
public string Address { get; set; }
public string Town { get; set; }
}
and I pass list of Companies to my view as:
#model IEnumerable<MyTestApp.Web.Models.Company>
....
#using (Html.BeginForm("Edit", "Shop"))
{
<table id="example">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Address)
</th>
<th>
#Html.DisplayNameFor(model => model.Town)
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model) {
<tr>
<td>
#Html.EditorFor(modelItem => item.Name)
</td>
<td>
#Html.EditorFor(modelItem => item.Address)
</td>
<td>
#Html.EditorFor(modelItem => item.Town)
</td>
</tr>
}
</tbody>
</table>
<input type="submit" value="Submit" />
}
And everything looks ok, but I can't understand how to get modified data in the controller? I used these approaches:
public ActionResult Edit(IEnumerable<Company> companies)
{
// but companies is null
// and ViewData.Model also is null
return RedirectToAction("SampleList");
}
I need access to modified objects, what am I doing wrong?
UPDATE: Thanks to webdeveloper, I just needed use 'for' loop instead of 'foreach' loop. Right version is
<tbody>
#for (int i = 0; i < Model.Count(); i++ ) {
<tr>
<td>
#Html.EditorFor(modelItem => modelItem[i].Name)
</td>
<td>
#Html.EditorFor(modelItem => modelItem[i].Address)
</td>
<td>
#Html.EditorFor(modelItem => modelItem[i].Town)
</td>
</tr>
}
</tbody>
Please, look at my answer here: Updating multiple items within same view OR for Darin Dimitrov answer.
You need items with index in name attribute in rendered html markup. Also you could look at: Model Binding To A List
I think that you are missing the Company's ID in your form so that the model can be correctly bound.
You should add it like this:
#using (Html.BeginForm("Edit", "Shop"))
{
<table id="example">
<thead>
<tr>
<th>
#Html.HiddenFor(model => model.ID)
#Html.DisplayNameFor(model => model.Name)
</th>
...
Otherwise the rest of your code seems to be OK.
You need to bind your table rows by providing an id for each one being edited so mvc can bind to it back to the controller. One row of table data example:
#for (var a = 0; a < #Model.Pets.Count; a++)
{
<tr>
<td>
#Html.CheckBoxFor(model => #Model.Pets[a].ChildSelected, new { #id= a + "childSelected" })
</td>
</tr>
i have following View where user has a table of products and i need select all data where "Quantity > 0" default is 0. But i dont know how can i get collection of data from table. Thanks for respond.
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Produkty</legend>
<table>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(model => item.Product.Name)
</td>
<td>
#Html.DisplayFor(model => item.Product.Price)
</td>
<td>
#Html.EditorFor(model => item.Quantity)
</td>
</tr>
}
<p>
<input type="submit" value="Orders" />
</p>
</table>
</fieldset>
}
//Controller
public ActionResult Index()
{
List<ProductListViewModel> productList = new List<ProductListViewModel>();
foreach (var item in db.Products.ToList())
{
ProductListViewModel model = new ProductListViewModel();
model.Product = item;
model.Quantity = 0;
productList.Add(model);
}
return View(productList);
}
Since you're using Html.EditorFor, things are easy.
Put productList as parameter in your Index Action(for Post), MVC will auto combine form data to productList Object, so you just need to filter the quantity in server side with a loop.
Of cause, to identify the product object, you'd better also add a hidden ID in your view.
<table>
#for(int i = 0; i<Model.Count; i++) {
<tr>
<td>
#Html.HiddenFor(model => Model[i].Product.ID)
#Html.DisplayFor(model => Model[i].Product.Name)
</td>
<td>
#Html.EditorFor(model => Model[i].Quantity)
</td>
</tr>
}
[HttpPost]
public ActionResult Index(List<ProductListViewModel> productList)
{
\\...
}