auto refresh of div in mvc3 ASP.Net not working - asp.net-mvc

ViewData.cshtml(Partial View)
This is partial View
<table id="users" class="ui-widget ui-widget-content" width="100%" align="center">
<thead>
<tr class="ui-widget-header ">
<th width="30%" align="center">Date</th>
<th width="30%" align="center">Bill Amount</th>
<th width="30%" align="center">PayNow</th>
</tr>
</thead>
<tbody>
#{
for (int i = #Model.bill.Count - 1; i >= 0; i--)
{
<tr>
<td width="30%" align="center">#Model.billdate[i]</td>
<td width="30%" align="center">#Model.bill[i]</td>
<td width="30%" align="center">
<a class="isDone" href="#" data-tododb-itemid="#Model.bill[i]">Paynow</a>
</td>
</tr>
}
}
</tbody>
</table>
Index.cshtml(View)
This is my view
<script type="text/javascript">
$(document).ready(function () {
window.setInterval(function () {
var url = '#Url.Action("ShowScreen", "Home")';
$('#dynamictabs').load(url)
}, 9000);
$.ajaxSetup({ cache: false });
});
</script>
<div class="dynamictabs">
#Html.Partial("ShowScreen")
</div>
HomeController.cs(Controller)
This is home controller
public ActionResult Index()
{
fieldProcessor fp= new fieldProcessor();
return View(fp);
}
public ActionResult ShowScreen()
{
return View();
}
fieldProcessor.cs
My Model
public class fieldProcessor
{
public List<int> bill { get; set; }
public List<string> billdate { get; set; }
}
Still Div is not getting refreshed.

This line:
$('#dynamictabs').load(url)
Should be:
$('.dynamictabs').load(url)
...because your div has a class of dynamictabs, not an id.

It will work with some code changes:
1) like greg84 said:
This line:
$('#dynamictabs').load(url)
Should be:
$('.dynamictabs').load(url)
2) Your controller should be like this:
public ActionResult ShowScreen()
{
fieldProcessor fp = new fieldProcessor();
/*Load fieldProcessor object*/
return View(fp);
}
public ActionResult Index()
{
fieldProcessor fp = new fieldProcessor();
/*Load fieldProcessor object*/
return View(fp);
}
3) ShowScreen and Index should be strong typed views:
#model YourApp.Models.fieldProcessor
4) When your code call the partial view, you should pass the model, like this:
<div class="dynamictabs">
#Html.Partial("ShowScreen",Model)
</div>

Related

Umbraco httppost error: "No parameterless constructor defined for this object"

I want to send a model to the action of the HomePost controller, I shot UmbracoForm and surface controller, when I run it and I click submit I get the error: "No parameterless constructor defined for this object".
ViewModel:
namespace Umbraco12.Models
{
public class Home : RenderModel
{
public Home(IPublishedContent content, CultureInfo culture) : base(content, culture)
{
}
public string Topic { get; set; }
}
}
Controller:
public class HomeSurController : SurfaceController
{
// GET: HomeSur
[HttpPost]
public ActionResult HomePost(Home model)
{
//Do some stuff here, then return the base method
return View("Home", model);
}
}
View:
#inherits Umbraco.Web.Mvc.UmbracoViewPage<Umbraco12.Models.Home>
#using ContentModels = Umbraco.Web.PublishedContentModels;
#{
Layout = "Master.cshtml";
}
#using (Html.BeginUmbracoForm("HomePost", "HomeSur", System.Web.Mvc.FormMethod.Post, new { #class = "YourclassName" }))
{
<table cellpadding="0" cellspacing="0">
<tr>
<th colspan="2" align="center">Person Details</th>
</tr>
<tr>
<td>#Umbraco.Field("topic")</td>
<td>
#Html.TextBoxFor(m => m.Topic)
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
}

Get and Set Data CheckboxList on Table in Asp Core 2.2

I wrote a class and set public IActionResult Index():
public class SuperBowl
{
public string Name { get; set; }
public double Price { get; set; }
public bool GiftPackage { get; set; }
}
[HttpGet]
public IActionResult Index()
{
double[] pricearraying = { 630, 500.25, 385.75 };
string[] namearraying = { "Red", "Green", "Blue" };
var heyinitial = new List<SuperBowl>();
for (int i = 0; i < pricearraying.Length; i++)
{
heyinitial.Add(new SuperBowl { Name = namearraying[i], Price = pricearraying[i], GiftPackage = false });
}
return View(heyinitial);
}
[HttpPost]
public IActionResult Index(List<SuperBowl> ceko)
{
double[] exposalary = { 63.00, 42.00, 21.00 };
var choosing = new List<double>();
foreach (var item in ceko.Select(x=>x.GiftPackage))
{
if (item == true)
{
choosing.Add(exposalary[ceko.FindIndex(r=>r.GiftPackage==item)]);
}
}
}
return View("MyCheckOut",choosing);
}
And Here is my Index.cshtml:
#model List<SuperBowl>
#{
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<form asp-action="Index" asp-controller="Home" method="post">
<div class="table-responsive-sm table-hover">
<table align="center" class="table table-bordered">
<thead>
<tr class="table-light text-center">
<th>Item Name</th>
<th>My Price</th>
<th>Hey Gift</th>
</tr>
</thead>
<tbody>
#foreach (var st in Model)
{
<tr class="text-center">
<td asp-for="#st.Name">#st.Name</td>
<td asp-for="#st.Price">#st.Price</td>
<td asp-for="#st.GiftPackage">#Html.CheckBoxFor(m => st.GiftPackage)</td>
</tr>
}
</tbody>
</table>
<input type="submit" value="CheckOut" class="btn btn-primary" />
</div>
</form>
</body>
</html>
And Here is MyCheckOut.cshtml:
#model List<double>
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Hello</title>
</head>
<body>
<div class="table-responsive-sm table-hover">
<table align="center" class="table table-bordered">
<thead>
<tr class="table-light text-center">
<th>Our Discount Numbers</th>
</tr>
</thead>
<tbody>
#foreach (var discounting in Model)
{
<tr class="text-center">
<td>#discounting</td>
</tr>
}
</tbody>
</table>
</div>
</body>
</html>
My expect is that when I tick only Red which indice is 0, then via the same indice 0, let's get array exposalary[0] which is 63.00
or when I tick Red and Blue which these indices are 0 and 2, then via the same indices 0 and 2, let's get array exposalary[0] and exposalary[2] which are 63 and 21.
If I deploy all these codes, in Index.cshtml my table seems good and every data is Ok on the other hand when I choose any checkbox submitting and send my choice in MyCheckOut, there is no element.
Please Help Me.
There are several problems in your codes , for example it's GiftPackage not GiftPacage in your page .
The key to complex object binding is ensuring that a sequential index in square brackets is added to the form field's name attribute in your case . So modify your Index.cshtml as :
#for (int i = 0; i < Model.Count; i++)
{
<tr class="text-center">
<td asp-for="#Model[i].Name">#Model[i].Name</td>
<td asp-for="#Model[i].Price">#Model[i].Price</td>
<td asp-for="#Model[i].GiftPackage">#Html.CheckBoxFor(m => Model[i].GiftPackage)</td>
</tr>
}
Another problem is The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. So you will get multi same value if multi selection is made from checkbox . Update your action code to :
[HttpPost]
public IActionResult Index(List<SuperBowl> ceko)
{
double[] exposalary = { 63.00, 42.00, 21.00 };
var choosing = new List<double>();
for (int i = 0; i < ceko.Count; i++)
{
if (ceko[i].GiftPackage ==true)
{
choosing.Add(exposalary[i]);
}
}
return View("MyCheckOut", choosing);
}

ASP.NET MVC CheckBox List sending always null

I want to create a table with items and checkboxes to select these items. I am including these checkboxes in a list but this list is always returning null. Thank you very much for your help.
Controller:
public ActionResult Index()
{
var expenseTypesView = new ViewExpenseTypesAndSelection
{
ExpensesTypes = _context.ExpenseType.ToList(),
};
return View(expenseTypesView);
}
[HttpPost]
public ActionResult SelectExpensesTypes( ViewExpenseTypesAndSelection SelectedExpenses)
{
var entry = new userSpecificExpenses();
var i = 0;
foreach (var item in SelectedExpenses.SelectedExpenses)
{
if(item)
{
entry.expenseTypeId = i;
entry.UtilisateurId= User.Identity.GetUserId();
_context.UserspecificExpenses.Add(entry);
_context.SaveChanges();
}
i++;
}
return View();
}
View:
#model MBT.ViewModels.ViewExpenseTypesAndSelection
....
#using (Html.BeginForm("SelectExpensesTypes", "ExpenseTypes"))
{
<table class="table table-bordered table-hover">
<thead>
<tr>
<th class="text-center">Expense</th>
<th class="text-center">Include</th>
</tr>
</thead>
<tbody>
#{
for (int i = 0; i < Model.ExpensesTypes.Count; i++)
{
<tr>
<td>
<div class="checkbox">
#Model.ExpensesTypes[i].Type
</div>
</td>
<td>
<div class="checkbox">
#Html.CheckBoxFor(m => m.SelectedExpenses[i])
</div>
</td>
</tr>
}
}
</tbody>
</table>
<button type="submit" class="btn btn-primary">Submit</button>
}
You sending ViewExpenseTypesAndSelection almost null just with ExpensesTypes and then post it back to your post method. It means you send null and receive null.
So what is wrong with that?

Posted model is null

I'm not sure what's wrong since I'm very new with MVC. This is a shopping cart. The customer is able to review their cart and edit quantity.
On the HttpPost ViewCart method, the cart is always empty, and number of lines is zero.
Controller:
public ActionResult ViewCart() {
var cart = (CartViewModel)Session["Cart"];
return View(cart);
}
[HttpPost]
public ActionResult ViewCart(CartViewModel cart) {
Session["Cart"] = cart;
return RedirectToAction("Order", "Checkout");
}
View:
#model CartViewModel
using (Html.BeginForm()) {
<h2>Your cart</h2>
<table>
<thead> ... </thead>
<tbody>
#foreach (var item in Model.Lines) {
<tr>
<td>#Html.DisplayFor(modelItem => item.Article.Description)</td>
<td>#Html.EditorFor(modelItem => item.Quantity)</td>
</tr>
}
</tbody>
</table>
<input type="submit" value="Checkout">
}
ViewModel:
public class CartViewModel {
public List<Line> Lines { get; set; }
public CartViewModel() {
Lines = new List<Line>();
}
}
Try changing the view to use indexes:
#model CartViewModel
using (Html.BeginForm()) {
<h2>Your cart</h2>
<table>
<thead> ... </thead>
<tbody>
#for (int i = 0; i < Model.Lines.Count; i++) {
<tr>
<td>#Html.DisplayFor(m => Model.Lines[i].Article.Description) #Html.HiddenFor(m => Model.Lines[i].Article.Id)</td>
<td>#Html.EditorFor(m => Model.Lines[i].Quantity)</td>
</tr>
}
</tbody>
</table>
<input type="submit" value="Checkout">
}

fiil a list with values of a table

I'm new in learning asp.net MVC. I am writing because I am stubborn to a problem. Indeed, I have an application that should allow me to create an XML file that will be added to a database. At this point, I created my Model, and my view that allows me to create my XML tags.
I saw on this site that could add lines in my table via Javascript. What I have done just as you can see in the code.
I can not recover what is the value of each line that I can insert. Passing my view a list I created myself. I can recover both inputs I inserted in my controller.
My question is, there's another way to create a dynamic lines via javascript, then all the entries that the user has entered the recover and fill in my list? Then I know myself how I can play with my list. But I just want to recover all the different lines that my user has inserted. I am new in ASP.NET MVC. Any help , please
This is my code.
Model
public class XMLFile
{
public string TypeDoc { get; set; }
public string Type { get; set; }
public string Contenu { get; set; }
public string DocName { get; set; }
}
This is my controller :
public class XMLFileController : Controller
{
List<XMLFile> file = new List<XMLFile>();
[HttpGet]
public ActionResult Save()
{
file.AddRange( new XMLFile[] {
new XMLFile (){Type = "Titre", Contenu = "Chef de Service"},
new XMLFile (){Type = "Item", Contenu="Docteur Joel"}
});
return View(file);
}
[HttpPost]
public ActionResult Save(List<XMLFile> formCollection)
{
try
{
if (formCollection == null)
{
return Content("la liste est nulle");
}
else
{
return RedirectToAction("Create", "Layout");
}
}
catch
{
return View();
}
}
}
My view with a script for adding a new Row :
#using (Html.BeginForm("Save", "XMLFile", FormMethod.Post,new { #class = "form-horizontal", #role = "form", #id = "FormCreateXML" }))
{
<table class="table table-bordered" id="XMLFileTable">
<thead>
<tr>
<th>Type</th>
<th>Contenu</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
#for (int i = 0; i<Model.Count; i++)
{
<tr>
<td>#Html.TextBoxFor(model=>model[i].Type, new {#class="form-control help-inline", #placeholder="type" })</td>
<td> #Html.TextBoxFor(model=>model[i].Contenu, new {#class="form-control help-inline", #placeholder="contenu" })</td>
<td> <input type="button" class="BtnPlus" value="+" /> </td>
<td> <input type="button" class="BtnMinus" value="-" /> </td>
</tr>
}
</tbody>
<tfoot>
<tr>
<td> <button type="submit" class="btn btn-success" >Save</button> </td>
</tr>
</tfoot>
</table>
}
</body>
<script type="text/javascript">
$(document).ready(function () {
function addRow() {
var html = '<tr>' +
'<td><input type="text" class="form-control" placeholder="type"></td>' +
'<td> <input type="text" class="form-control" placeholder="contenu"></td>' +
'<td> <input type="button" class="BtnPlus" value="+" /> </td>' +
'<td> <input type="button" class="BtnMinus" value="-" /></td>' +
'</tr>'
$(html).appendTo($("#XMLFileTable"))
};
function deleteRow() {
var par = $(this).parent().parent();
par.remove();
};
$("#XMLFileTable").on("click", ".BtnPlus", addRow);
$("#XMLFileTable").on("click", ".BtnMinus", deleteRow);
});
</script>

Resources