I am trying out Editor Template
My Model Class is
public class ResourceModel
{
public int ResourceId { get; set; }
public string ResourceName { get; set; }
public int CultureId { get; set; }
public string CultureName { get; set; }
public string Icon { get; set; }
public string Language { get; set; }
public int CulturalResourceId { get; set; }
public string ResourceValue { get; set; }
public int ResourceAreaId { get; set; }
public int ViewId { get; set; }
public string WebView { get; set; }
public int WebAreaId { get; set; }
public string AreaName { get; set; }
public string FinalResourceValue { get; set; }
public List<ResourceModel> ListResourceCulture { get; set; }
}
I have created a partial view for Editor Template and put that in a EditorTemplates folder inside Shared Folder with name ResourceModel.cshtml.The Following is my Editor Template
#model Mars.Model.ResourceModel
#{
Layout = null;
}
#Html.HiddenFor(m=>m.CultureId)
#Html.HiddenFor(m => m.CulturalResourceId)
#Html.DisplayFor(m => m.Language, new { #class="form-control"})
#Html.TextBoxFor(m => m.ResourceValue, new { #class="form-control"})
and I used #Html.EditorFor in the view page as
1.For the Create View
#Html.EditorFor(Model => Model.ListResourceCulture)
2.For the Edit View
#Html.EditorFor(Model => Model.ListResourceCulture)
These two views belong to same controller with different actions
The template works for the create page but the template doesn't works for the Edit page. The template does not get triggered.
Related
I have customer model
public class Customer
{
public Customer()
{
this.SystemValues = new HashSet<SystemValue>();
}
public string Name { get; set; }
public Nullable<System.Guid> GUID { get; set; }
public int Id { get; set; }
public virtual ICollection<SystemValue> SystemValues { get; set; }
}
and systemValue model
public class SystemValue
{
public int CustomerId { get; set; }
public int SystemValueId { get; set; }
public Nullable<int> SystemValueCategoryId { get; set; }
public string SystemValueType { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string TextValue { get; set; }
public Nullable<int> IntValue { get; set; }
public Nullable<double> FloatValue { get; set; }
public byte[] BlobValue { get; set; }
public Nullable<System.DateTime> DateTimeValue { get; set; }
public Nullable<bool> BooleanValue { get; set; }
public Nullable<int> LookupValueId { get; set; }
public Nullable<int> LookupValueGroupId { get; set; }
public Nullable<bool> IsReadonly { get; set; }
public bool IsHidden { get; set; }
public int Id { get; set; }
public virtual Customer Customers { get; set; }
}
in which way I could show a link in CustomerView(CustomersController) foreach customer that redirect to the SystemValuesView(SystemValuesController) with related to this customer SystemValues?
I found out one way - redirect to this controller's action with parameter.
public ActionResult ViewSystemValues(int? id)
{
return RedirectToAction("Index", "SystemValues", new {id});
}
But I'm sure there must be smarter way.
#for(int i = 0; i < Model.yourcustomerlist.Count(); i++)
{
<tr>
<td>
<a class="btn btn-primary" href="#Url.Action("Index", "SystemValues", new { id = Model.yourcustomerlist[i].CustomerId })">
<b>Go to system values</b>
</a>
</td>
</tr>
}
I hope I understood you correctly.
This code should go in the view. The view should be strongly typed to a model.
Code is if you want a button that redirects to the index view of the SystemValues controller, with the CustomerId as input. You should change "yourcustomerlist" to the list containing the customer information. If it's not part of a table, remove the table-related tags (<td> and <tr>).
I am new in MVC. I have tried to read online articles about how to use display template in collection.
This is my Model
public class InsuranceListViewModel
{
public IList<InsuranceViewModel> InsuranceViewModelList { get; set; }
public InsuranceViewModel InsuranceViewModel { get; set; }
public List<InsuranceSummaryViewModel> InsuranceSummaryViewModelList { get; set; }
}
SummaryViewModel
public class InsuranceSummaryViewModel
{
public string FriendlyName { get; set; }
public int InsuranceId { get; set; }
public string GroupId { get; set; }
public string MemberId { get; set; }
public Nullable<System.DateTime> PolicyStartDate { get; set; }
public Nullable<System.DateTime> PolicyEndDate { get; set; }
}
InsurnaceDisplay.cshtml
#model P2PExchange.MVC.Web.Areas.Patient.Models.InsuranceSummaryViewModel
#Html.DisplayFor(x => x.InsuranceId)
#Html.DisplayFor(x => x.FriendlyName)
#Html.DisplayFor(x => x.GroupId)
#Html.DisplayFor(x => x.MemberId)
#Html.DisplayFor(x => x.PolicyStartDate)
#Html.DisplayFor(x => x.PolicyEndDate)
_Insurance.cshml
#model Models.InsuranceListViewModel
#Html.DisplayFor(x=>x.InsuranceSummaryViewModelList)
My above code it only displays insurance friendly name not all fields from display template.
Can someone please help/guide me how Can I display correct data on UI?
Your DisplayTemplate needs to be located in the /Views/Shared/DisplayTemplates or /Views/yourControllerName/DisplayTemplates folder and needs to be named to match the name of the class, i.e. InsuranceSummaryViewModel.cshtml
UserProfiles Model
[Table("Users")]
public class UserProfiles
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
//public string Password { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public string Initials { get; set; }
public string Company { get; set; }
public string Department { get; set; }
public string Title { get; set; }
public string Team { get; set; }
public string TeamSub { get; set; }
public string Phone { get; set; }
public string ImageLocation { get; set; }
public string Note { get; set; }
public string Comment { get; set; }
}
Controller
public ActionResult Index()
{
**EDIT :** ViewBag.ProfileId = new SelectList(db.UserProfiles, "UserName", "UserName");
return View(db.UserProfiles.ToList());
}
In the View ( i hope / believe here is the issue )
#model IEnumerable<OilNGasWeb.Models.UserProfiles>
#{
ViewBag.Title = "CLS - Oil & Gas Web Site Users";
}
<h2>Web Site Users</h2>
**Removed** #Html.DropDownList(Model => Model.UserName,Model.UserName)
**Changedinto** #Html.DropDownList("UserName", String.Empty)
New Error:
Exception Details: System.InvalidOperationException: There is no ViewData item of
type 'IEnumerable<SelectListItem>' that has the key 'UserName'.
Add a SelectList to your ViewBag in the Controller:
ViewBag.ProfileId = new SelectList(db.UserProfiles, "Id", "Username");
then add this in your View:
#Html.DropDownList("ProfileId", String.Empty)
Moreover you should have used "model" instead of "Model" in your lambda expression.
here is my model
namespace chPayroll.Models.CustInformations
{
public class CustContact
{
public int cId { get; set; }
public int cNoType { get; set; }
public string cNo1 { get; set; }
public string cNo2 { get; set; }
public string cNo3 { get; set; }
public List<CustContact> contact { get; set; }
}
}
here is my editorTemplates
#model chPayroll.Models.CustInformations.CustContact
#Html.TextBoxFor(model => model.cNo1)
#Html.TextBoxFor(model => model.cNo2)
#Html.TextBoxFor(model => model.cNo3)
I need to show three textbox for taking email,three text box for taking telephone no. in view. how can I add the items to the list contact defined in the model so that it shows like this
email:--textbox1----textbox2----textbox3--
telephone:--textbox1----textbox2----textbox3--
and sends value to the controller
actually I am trying to send my data in list named contact here ie inside list at
index 0-email1-email2-email3
index 1-tel1-tel2-tel3
#Sanjay: you have a strange construct in your view model:
public class CustContact
{
public List<CustContact> contact;
}
Even if it compiles and machine understands it, I wouldn't use it as it is - you trying to lift yourself from the ground by pulling your hair up :)
It should be defined something along these lines (following your naming conventions & logic):
public class CustContact // single
{
public int cId { get; set; }
public int cNoType { get; set; }
public string cNo1 { get; set; } // those are actual phones, emails etc data
public string cNo2 { get; set; }
public string cNo3 { get; set; }
}
public class CustContacts // plural
{
public List<CustContact> Contacts;
}
View:
#model CustContacts
#EditorFor(m => Model)
Editor template:
#model CustContact
#Html.EditorFor(m => m.cNo1)
#Html.EditorFor(m => m.cNo2)
#Html.EditorFor(m => m.cNo3)
For brevity, we don't deal here with annotations, decorations, error handling etc.
Hope this helps.
Based on the comment on the question, I would build the models as below
public class CustContact
{
public int cId { get; set; }
public int cNoType { get; set; }
public string cNo1 { get; set; }
public string cNo2 { get; set; }
public string cNo3 { get; set; }
}
public class Customer
{
public CustContact Email {get; set;}
public CustContact Telephone {get; set;}
}
then create an editor template for Customer and in that editor template have following logic
#Html.EditorFor(model => model.Email)
#Html.EditorFor(model => model.Telephone)
Hope this helps
My problem started when I wrap three classes
the first class is
[Bind(Exclude="ID")]
public class Material
{
public string MaterialName { get; set; }
}
the second class is
[Bind(Exclude="ID")]
public class ExameState
{
public string ExamState { get; set; }
}
the third class is
[Bind(Exclude="ID")]
public class Exams
{
public string ExamsName { get; set; }
public DateTime CreationDate { get; set; }
public DateTime StartingDate { get; set; }
public int Period { get; set; }
public int ExamStateID { get; set; }
public int MaterialID { get; set; }
public int GroupID { get; set; }
public int QuestionState { get; set; }
public int TeacherID { get; set; }
public int ExamMarkState { get; set; }
public string Password { get; set; }
}
wrapper class is
public class Examswrapper
{
public Material material { get; set; }
public ExameState examstate { get; set; }
public Exam exam { get; set; }
}
I need to display dropdownlist for Material with datavalue=MaterialName
and key=ID in view build on Examswrapper class
I am trying this
how to make it
and thank you for your advice
new error :
System.NullReferenceException: Object reference not set to an instance of an object.
There is a problem with your view model because the Material property should be a collection if you want a drop down list. Also your Material class is missing an ID property. So:
public class Material
{
public string ID { get; set; }
public string MaterialName { get; set; }
}
public class ExamsViewModel
{
public string SelectedMaterialId { get; set; }
public IEnumerable<Material> Materials { get; set; }
... // some other properties
}
and then in your strongly typed view:
<%= Html.DropDownListFor(
// This is the property that will hold the selected value
x => x.SelectedMaterialId,
// Build the select list based on the Model.Materials collection
new SelectList(
Model.Materials.Select(material => new SelectListItem
{
Value = material.ID,
Text = material.MaterialName
}),
"Value", "Text"
)
) %>
#Html.DropDownListFor(model => model.material,
Model.material.Select(
x => new SelectListItem
{
Text = x.MaterialName,
Value = x.Id
}
))