i'm using Razor with MVC3 in my project. I use membership to handle user registration and i want to diplay all my users into a table .
here's my Action:
public ActionResult ListProfile()
{
//ProfileInfoCollection profiles = ProfileManager.GetAllProfiles(ProfileAuthenticationOption.All);
//return View(profiles);
var users = Membership.GetAllUsers();
return View(users);
}
My View :
#inherits System.Web.Mvc.ViewPage<MembershipUserCollection>
#{
ViewBag.Title = "Documents";
}
<h2>Liste des documents</h2>
<table style="width: 100%;">
<tr>
<th>Nom
</th>
</tr>
#foreach (MembershipUser item in Model)
{
<tr>
<td>
<h4>
#item.UserName
</h4>
</td>
</tr>
}
</table>
But i get an error : CS0115: 'ASP._Page_Views_AccountProfile_listProfile_cshtml.Execute()': no suitable method found to override
Have to change the basetype to System.Web.Mvc.WebViewPageinstead of System.Web.Mvc.ViewPage because razor configuration is under ~/Views/Web.config
Here is the view :
#inherits System.Web.Mvc.WebViewPage<MembershipUserCollection>
#{
ViewBag.Title = "Documents";
}
<h2>Liste des documents</h2>
<table style="width: 100%;">
<tr>
<th>Nom
</th>
</tr>
#foreach (MembershipUser item in Model)
{
<tr>
<td>
<h4>
#item.UserName
</h4>
</td>
</tr>
}
</table>
Related
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.
Imported data not displays in repeated table. It's only shows demo data. below is my template.
#{
<table class="table table-striped table-bordered">
<tbody>
#foreach (var Content in AsDynamic(Data["Default"]).OrderBy(m => m.wcName))
{
<tr>
<td width="30%">#Content.wcName </td>
<td>#Html.Raw(#Content.wcPosition)
#if (DotNetNuke.Common.Globals.IsEditMode())
{
#Content.Toolbar
}
</td>
</tr>
}
</tbody>
</table>
}
Your template only shows items assigned to this page/module, not "all data" - for that you would have to replace Data["Default"] with App.Data["your-content-type-name"].
You probably want to read more about this here: http://2sxc.org/en/blog/post/12-differences-when-templating-data-instead-of-content
I am using jquery datatables in my MVC ASP.Net application and get an alert warning while the application is running. The alert is "Warning: Scroller requires DataTables 1.10.0 or greater". I have verified that my version of DataTables is 1.10.10 and the DataTables work fairly well except for a few issues.
My issues with DataTables is when pages using it initially load, the number of rows that are supposed to be displayed do not match how many are shown. All rows are shown on a page load. Once I interact with the table, everything is fixed.
I am loading the javascript and css in my BundleConfig.cs file.
One of my views is as follows:
#model IEnumerable<Q5.ViewModels.ProjectVM>
#{
ViewBag.Title = "Projects";
}
<h2>Projects/Deals</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table table-striped table-hover display" id="projects" cellspacing="0">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayName("Last Updated")
</th>
<th>
#Html.DisplayName("Sales Person")
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.ActionLink(item.Name, "ProjectProfile", new { id = item.Id })
</td>
<td>
#Html.DisplayFor(modelItem => item.LastUpdated)
</td>
<td>
#Html.DisplayFor(modelItem => item.AssignedToUserName)
</td>
</tr>
}
</tbody>
</table>
<script>
$(document).ready(function () {
$('#projects').DataTable();
})
</script>
You could have the table refreshed after load:
$('#example').dataTable( {
"initComplete": function(settings, json) {
alert( 'DataTables has loaded.' );
$('#example').dataTable().ajax.reload( null, false );
}
} );
This may not be 100% syntax, just typed off the cuff. I seem to remember having to do this for a project, but the details escape me. I never could figure out why it happened so I slapped this band aid on it.
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>