<table>
#foreach (DataRow row in Model.Rows)
{
<tr>
#foreach (DataColumn col in Model.Columns)
{
<td>#row[col.ColumnName]</td>
}
</tr>
}
</table>
#Html.ActionLink("Pass", "Insert", "Home")
When clicking the pass link i would like the columns to be inserted into db. Is there any tutorial on how to do this?
Please try this sample.
Create a view model class as follows
public class DataRows
{
public int Column1{ get; set; }
public string Column2 { get; set; }
public double Column3 { get; set; }
}
public class MyModel
{
public virtual DataRows[] DataSet { get; set; }
}
Retrieve all the data using your business logic into MyModel in your controller. I am demonstrating this using a sample code
public TestModel GenerateModel()
{
var model = new TestModel();
model.DataSet = this.GenerateDataGridData().ToArray();
return model;
}
private ICollection<SampleDataSet> GenerateDataGridData()
{
var list = new List<SampleDataSet>()
{
new SampleDataSet() { Column1= 1, Column2= "XXX", Column3= 23 },
new SampleDataSet() { Column1= 2, Column2= "YYY", Column3= 27 },
new SampleDataSet() { Column1= 3, Column2= "ZZ", Column3= 25 }
};
return list;
}
In the Action of your view you will be calling GenerateModel method as follows
public ActionResult MyView()
{
var model = this.GenerateModel();
return this.View(model);
}
Now you restructure your MyView page as follows
#model MyModel
#{
int i = 0;
}
#using (Html.BeginForm("Insert", "Home", FormMethod.Post))
{
<table>
<thead>
<tr>
<th>Column1</th>
<th>Column2</th>
<th>Column3</th>
</tr>
</thead>
<tbody>
#foreach (var row in Model.DataSet)
{
<tr>
<th>#row.Column1<span>#Html.HiddenFor(x => Model.DataSet[i].Column1)</span></th>
<th>#row.Column2<span>#Html.HiddenFor(x => Model.DataSet[i].Column2)</span></th>
<th>#row.Column3<span>#Html.HiddenFor(x => Model.DataSet[i].Column3)</span></th>
</tr>
i++;
}
</tbody>
</table>
<input type="submit" value="Pass" />
}
Now on submiting the data in the view will be posted to your Insert Action of Home controller and you can insert into your DB using your logic
I hope this will be a solution for your query.
Related
I want to use Razor helper in .net core
I use this code
#{
void DrawRow(CategoryModel child, int parentNo)
{
#<text>
<tr style="display: none;">
<td><span class="treegrid-indent"></span><span class="treegrid-expander"></span>#child.Name</td>
</td>
</tr>
</text>;
}
}
but when use this , get error
The "#" character must be followed by a ":", "(", or a C# identifier. If you intended to switch to markup, use an HTML start tag,
You can achieve same requirement using a view component in ASP.NET Core application, like below.
ViewComponent class
[ViewComponent(Name = "DrawRow")]
public class DrawRowComponent : ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync(DrawRowModel model)
{
return View(model);
}
}
ViewComponent Razor view
#model DrawRowModel
#*display: none;*#
<tr style="">
<td>
<span class="treegrid-indent"></span>
<span class="treegrid-expander"></span>
#Model.child.Name
</td>
</tr>
Model Class(es)
public class CategoryModel
{
public string Name { get; set; }
}
public class DrawRowModel
{
public CategoryModel child { get; set; }
public int parentNo { get; set; }
}
Invoke view component in Test View page
#{
ViewData["Title"] = "Test";
var model = new DrawRowModel { child = new CategoryModel { Name = "Category" }, parentNo = 0 };
}
<h1>Test</h1>
#for (int i = 1; i < 6; i++)
{
model.child.Name = "Category" + i.ToString();
model.parentNo = i;
<table>
#await Component.InvokeAsync("DrawRow", model)
</table>
}
Test Result
I want to view the table in Edit.cshtml
I was using the Html.Action to get the table from SurveyQuestionController but it shows the error as below:
The model item passed into the dictionary is of type 'System.Collections.Generic.List``1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[SurveyTool.Models.SurveyQuestionModel]'.
Anything that i'm wrong??
Edit.cshtml:
#model SurveyTool.Models.SurveyModel
<div>
<h2>Link</h2>
#Html.Action("QuestionLink", "SurveyQuestion", new { id = Model.SurveyID })
</div>
SurveyQuestionController.cs:
public ActionResult QuestionLink(string SurveyID)
{
var query = (from r in db.SurveyQuestionModels
where r.SurveyId == SurveyID
select r.QuestionLink).Distinct();
return PartialView(query.ToList());
}
QuestionLink.cshtml :
#model IEnumerable<SurveyTool.Models.SurveyQuestionModel>
<br />
<table class="strip">
#foreach (var item in Model)
{
<tr>
<td>#Html.DisplayFor(modelitem => item.QuestionLink)</td>
</tr>
}
</table>
SurveyQuestionModel.cs :
namespace SurveyTool.Models
{
public class SurveyQuestionModel
{
public int Id { get; set; }
public string QuestionLink { get; set; }
}
}
You query is selecting only the QuestionLink property of SurveyQuestionModel (by you use select r.QuestionLink) which is typeof string, hence your returning List<string> to the view, when the view expects List<SurveyQuestionModel>
Change the query to return a collection of SurveyQuestionModel
public ActionResult QuestionLink(string SurveyID)
{
var query = (from r in db.SurveyQuestionModels
where r.SurveyId == SurveyID
select r);
return PartialView(query.ToList());
}
or change the view so the model is List<string>
#model List<string>
#foreach (var item in Model)
{
#Html.DisplayFor(modelitem => item)
}
XOn an ASP.NET MVC Website I´m posting a List of models, wrapped in a classe's property, by submitting a form.
When the "List propertys" name contains a
capital letter beside the first one, deserialization seems to fail when posting back, hence the List property of the wrapping class received as parameter in the controller method is null. If the propertie`s name doesn't contain any further capital letters all going fine.
For better comprehension I created an example with two wrapping classes Department & DepartmentX:
namespace CollectionPropertyCamelCase.Models
{
public class Employee
{
public string Name { get; set; }
public int Age { get; set; }
}
public class Department
{
public IList<Employee> Employees { get; set; }
}
public class DepartmentX
{
public IList<Employee> EmployeesX { get; set; }
}
}
namespace CollectionPropertyCamelCase.Controllers
{
public class DepartmentController : Controller
{
// ---> Works fine
public ActionResult Index()
{
var d = new Department()
{
Employees = new List<Employee>()
{
new Employee(){ Name = "Paul", Age = 35},
new Employee(){ Name = "Laura", Age = 27},
new Employee(){ Name = "John", Age = 99}
}
};
return View(d);
}
[HttpPost]
public ActionResult Index(Department d)
{
// All fine: Parameter d`s poperty Employees is an instance of
// List<Employee> conatining three instances of Employee
// providing the values as defined in Index().
return View(d);
}
// <--- Works fine
// ---> Doesn't work
public ActionResult IndexX()
{
var d = new DepartmentX()
{
// this is the troublemaker: Usage of 'X' in the propertyname
EmployeesX = new List<Employee>()
{
new Employee(){ Name = "Paul", Age = 35},
new Employee(){ Name = "Laura", Age = 27},
new Employee(){ Name = "John", Age = 99}
}
};
return View(d);
}
[HttpPost]
public ActionResult IndexX(DepartmentX d)
{
// Not so fine: Parameter d`s poperty EmployeesX is null
return View(d);
}
// <--- Doesn't work
}
}
#* Index.cshtml *#
#model CollectionPropertyCamelCase.Models.Department
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
#using (Html.BeginForm())
{
<table>
#for (int i = 0; i < Model.Employees.Count; i ++)
{
<tr>
<td>
#Html.TextBoxFor(modelItem => Model.Employees[i].Name)
</td>
<td>
#Html.TextBoxFor(modelItem => Model.Employees[i].Age)
</td>
</tr>
}
</table>
<input type="submit" />
}
#* IndexX.cshtml *#
#model CollectionPropertyCamelCase.Models.DepartmentX
#{
ViewBag.Title = "IndexX";
}
<h2>IndexX</h2>
#using (Html.BeginForm())
{
<table>
#for (int i = 0; i < Model.EmployeesX.Count; i++)
{
<tr>
<td>
#Html.TextBoxFor(modelItem => Model.EmployeesX[i].Name)
</td>
<td>
#Html.TextBoxFor(modelItem => Model.EmployeesX[i].Age)
</td>
</tr>
}
</table>
<input type="submit" />
}
Does anyone have a clue how to influence deserialization, so camelcased propery names can be used at that place? Thanks in advance!
Please see below my Model class, my controller action and my view.
When I edit from my view I have got the exception :A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.
I have already asked this question, but I have got no answers; pls help!!
public partial class Organization : ILockable, IAuditable, IEntity
{
/*** Construtor(s) ***/
public Organization()
{
}
public Organization(Party obj)
: this()
{
Party = obj;
}
/*** Public Members ***/
[Key, Display(Name = "Id")]
public int PartyId { get; set; }
/* IEntity */
public string Caption { get; set; }
public string NameInUse { get; set; }
public string Description { get; set; }
/* IAuditable */
[NotMapped, ScaffoldColumn(false)]
public System.DateTimeOffset Created
{
get { return Party.Created; }
set { Party.Created = value; }
}
[NotMapped, ScaffoldColumn(false)]
public string CreatedBy
{
get { return Party.CreatedBy; }
set { Party.CreatedBy = value; }
}
[NotMapped, ScaffoldColumn(false)]
public Nullable<System.DateTimeOffset> LastMod
{
get { return Party.LastMod; }
set { Party.LastMod = value; }
}
[NotMapped, ScaffoldColumn(false)]
public string LastModBy
{
get { return Party.LastModBy; }
set { Party.LastModBy = value; }
}
[NotMapped, Display(Name = "Del?")]
public bool IsSoftDeleted
{
get { return Party.IsSoftDeleted; }
set { Party.IsSoftDeleted = value; }
}
[NotMapped, ScaffoldColumn(false)]
public Nullable<System.DateTimeOffset> SoftDeleted
{
get { return Party.SoftDeleted; }
set { Party.SoftDeleted = value; }
}
[NotMapped, ScaffoldColumn(false)]
public string SoftDeletedBy
{
get { return Party.SoftDeletedBy; }
set { Party.SoftDeletedBy = value; }
}
/* ILockable */
public string GetTableName()
{
return "Organization";
}
public int GetLockId()
{
return this.PartyId;
}
/* Navigation Properties */
/// <summary>
/// Foreign key to Party: PartyId
/// Organization is subtype of Party
/// </summary>
public virtual Party Party { get; set; }
}
Controller Edit Action:
[HttpPost]
public ActionResult Edit(Organization obj)
{
//remove the lock since it is not required for inserts
if (ModelState.IsValid)
{
OrganizationRepo.Update(obj);
UnitOfWork.Save();
LockSvc.Unlock(obj);
return RedirectToAction("List");
}
else
{
return View();
}
}
View:
#using PartyBiz.Models.Objects
#using d2Utils.Reflection
#model IEnumerable<Organization>
#{
ViewBag.Title = "Details";
}
<table>
<tr>
<th>
#Html.Raw("Caption")
</th>
<th></th>
</tr>
<tr>
<td colspan="4">
#foreach (var item in Model)
{
<table>
<tr>
#using (Html.BeginForm("Edit", "Organization", FormMethod.Post))
{
<td >
#Html.TextBox("Caption", item.GetValForProp<string>("Caption"), new { #class = "txt" })
</td>
<td >
#Html.TextBox("NameInUse", item.GetValForProp<string>("NameInUse"), new { #class = "txt" })
</td>
<td >
#Html.TextBox("Description", item.GetValForProp<string>("Description"), new { #class = "txt" })
</td>
<td>
<input type="hidden" name="PartyId" value="#item.PartyId"/>
<button type="submit">Edit</button>
</td>
}
</tr>
</table>
}
</td>
</tr>
</table>
Context Method:
public virtual void Update(T obj)
{
IAuditable audit = obj as IAuditable;
IOverTime overtime = obj as IOverTime;
// Existing entity
D2Repository.Updated(ref audit, UserName);
D2Repository.FromDate(ref overtime);
Set.Attach(obj);
Ctxt.Entry(obj).State = EntityState.Modified;
}
I have added
obj.Party.PartyId = obj.PartyId;
in my edit action and it is working now.
I still want to know if this is the correct way of doing it?
I'm trying to make a view model to show a list of checkboxes. A checkbox will be checked when its ID is found in the database. However, my code is generating an error.
CheckFacilityVN
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace XNet.WebUI.Hotel.ViewModel
{
public class CheckFacilityVM
{
public int FacilityID { get; set; }
public string facilityName { get; set; }
public List<FacilityAvailable> facilityAvailable { get; set; }
}
public class FacilityAvailable
{
public bool isCheck { get; set; }
}
}
My controller
public ActionResult Facility()
{
var htl = _hotelService.ShowRoomFacility(2);
var list = new List<FacilityAvailable>();
foreach (var x in htl)
{
list.Add(new FacilityAvailable { FacilityID = htl.FacilityID, facilityName = htl.FacilityName, isCheck = htl.IsActive });
}
return View();
}
My constructor
public Facility ShowRoomFacility(int HotelID)
{
var x = (from d in db.Facilities
where d.FacilityID == HotelID
select d).FirstOrDefault();
return x;
}
How can I make these checkboxes?
Start by adapting your view model:
public class CheckFacilityVM
{
public int FacilityID { get; set; }
public string FacilityName { get; set; }
public bool IsFacilityAvailable { get; set; }
}
and then use this view model:
public ActionResult Facility()
{
var model = _hotelService
.ShowRoomFacility(2)
.Select(htl => new CheckFacilityVM
{
FacilityID = html.FacilityID,
FacilityName = html.FacilityName,
IsFacilityAvailable = htl.IsActive,
})
.ToList();
return View(model);
}
and then write a corresponding view:
#model List<CheckFacilityVM>
#using (Html.BeginForm())
{
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Is available</th>
</tr>
</thead>
<tbody>
#for (var i = 0; i < Model.Count; i++)
{
<tr>
<td>
#Html.DisplayFor(x => x[i].FacilityID)
#Html.HiddenFor(x => x[i].FacilityID)
</td>
<td>
#Html.DisplayFor(x => x[i].FacilityName)
#Html.HiddenFor(x => x[i].FacilityName)
</td>
<td>
#Html.CheckBoxFor(x => x[i].IsFacilityAvailable)
</td>
</tr>
}
</tbody>
</table>
<button type="submit">Save</button>
}
and finally:
[HttpPost]
public ActionResult Facility(List<CheckFacilityVM> model)
{
// process the values from the view model here ...
}