Create TestModel class in the project show below code.
public class TestModel
{
public long EmailTemplateId { get; set; }
public string TemplateName { get; set; }
public string TemplateContent { get; set; }
public Nullable<System.DateTime> CreatedDate { get; set; }
public Nullable<System.DateTime> UpdatedDate { get; set; }
public Nullable<bool> IsDefaultTemplate { get; set; }
public string TemplateKey { get; set; }
public string TemplatePath { get; set; }
public string TemplateJson { get; set; }
public Nullable<bool> IsAdmin { get; set; }
public int TemplateType { get; set; }
public string TemplateTag { get; set; }
}
Create GetTestlst action method to the controller and get all data and list pass test.cshtml
public ActionResult GetTestlst()
{
var templist = GetTestList().ToList();
return View(templist);
}
below code written Test.cshtml page and get the model list in controller to view.
#using test.Helpers;
#model List<test.Entities.TestModel>
#{
ViewBag.Title = "Template";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model[0].EmailTemplateId)
</th>
<th>
#Html.DisplayNameFor(model => model[0].TemplateName)
</th>
<th>
#Html.DisplayNameFor(model => model[0].TemplateContent)
</th>
<th>
#Html.DisplayNameFor(model => model[0].CreatedDate)
</th>
<th>
#Html.DisplayNameFor(model => model[0].UpdatedDate)
</th>
<th>
#Html.DisplayNameFor(model => model[0].IsDefaultTemplate)
</th>
<th>
#Html.DisplayNameFor(model => model[0].TemplateKey)
</th>
<th>
#Html.DisplayNameFor(model => model[0].TemplatePath)
</th>
<th>
#Html.DisplayNameFor(model => model[0].TemplateJson)
</th>
<th>
#Html.DisplayNameFor(model => model[0].IsAdmin)
</th>
<th>
#Html.DisplayNameFor(model => model[0].TemplateType)
</th>
<th>
#Html.DisplayNameFor(model => model[0].TemplateTag)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.EmailTemplateId)
</td>
<td>
#Html.DisplayFor(modelItem => item.TemplateName)
</td>
<td>
**`<iframe id="testiframe" class="iframe" src=#item.TemplateContent></iframe>`**
</td>
<td>
#Html.DisplayFor(modelItem => item.CreatedDate)
</td>
<th>
#Html.DisplayFor(model => item.UpdatedDate)
</th>
<th>
#Html.DisplayFor(model => item.IsDefaultTemplate)
</th>
<th>
#Html.DisplayFor(model => item.TemplateKey)
</th>
<th>
#Html.DisplayFor(model => item.TemplatePath)
</th>
<th>
#Html.Raw(item.TemplateJson)
</th>
<th>
#Html.DisplayFor(model => item.IsAdmin)
</th>
<th>
#Html.DisplayFor(model => item.TemplateType)
</th>
<th>
#Html.DisplayFor(model => item.TemplateTag)
</th>
<td>
#*#Html.ActionLink("Edit", "Edit", "Test", new { id = item.Id }, null) |
#Html.ActionLink("Details", "Details", "Test", new { id = item.Id }, null) |
#Html.ActionLink("Delete", "Delete", "Test", new { id = item.Id }, null)*#
</td>
</tr>
}
</table>
Show output below image.
Iframe proper but not show Html content in the model list. please give the answer.
Have you considered loading the HTML into a container?
<div id='#item.EmailTemplateId'></div>
<script type="text/javascript">
$(document).ready(function(){
$('##item.EmailTemplateId').load('#item.');
});
</script>
Old Code replace to below code
<iframe id="testiframe" class="iframe" src=#item.TemplateContent></iframe>
I solved this issue
<div id=#item.EmailTemplateId style="height:200px; overflow:scroll;">#Html.Raw(HttpUtility.HtmlDecode(item.TemplateContent))</div>
Related
I have class connection
public partial class Connections
{
public System.Guid IdConnection { get; set; }
[Required(ErrorMessage = "Requienter code hereenter code here`red")]
public string Sign { get; set; }
[DataType(DataType.Date)]
[Required(ErrorMessage = "Required")]
public System.DateTime Date { get; set; }
[DataType(DataType.Time)]
[Required(ErrorMessage = "Required")]
public System.DateTime Time { get; set; }
[Required(ErrorMessage = "Required")]
public double Band { get; set; }
[Required(ErrorMessage = "Required")]
public string Modulation { get; set; }
[Required(ErrorMessage = "Required")]
public int RST { get; set; }
public string Comment { get; set; }
[Required(ErrorMessage = "Required")]
public string QLS { get; set; }
}
Method in controller:
[HttpPost]
public ActionResult DeleteMultiple(System.Guid[] deleteInputs)
{
var db= new MainDbContext();
var connections = db.Connections.ToList();
if (deleteInputs == null)
{
}
foreach (var item in deleteInputs)
{
Connections con = db.Connections.Find(item);
db.Connections.Remove(con);
}
db.SaveChanges();
return RedirectToAction("EditDelete", "Connection");
}
and View:
#model IEnumerable<RadioSite.Connections>
#{
ViewBag.Title = "My connections";
}
<table class="table">
<tr>
<th>
Select
</th>
<th>
#Html.DisplayNameFor(model => model.Sign)
</th>
<th>
#Html.DisplayNameFor(model => model.Date)
</th>
<th>
#Html.DisplayNameFor(model => model.Time)
</th>
<th>
#Html.DisplayNameFor(model => model.Band)
</th>
<th>
#Html.DisplayNameFor(model => model.Modulation)
</th>
<th>
#Html.DisplayNameFor(model => model.RST)
</th>
<th>
#Html.DisplayNameFor(model => model.Comment)
</th>
<th>
#Html.DisplayNameFor(model => model.QLS)
</th>
<th colspan="2">
Actions
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
<input type="checkbox" name="deleteInputs" id="deleteInputs" value="#item.IdConnection" />
</td>
<td>
#Html.DisplayFor(modelItem => item.Sign)
</td>
<td>
#Html.ValueFor(modelItem => item.Date, "{0:dd/MM/yyyy}")
</td>
<td>
#Html.ValueFor(modelItem => item.Time, "{0:HH:MM}")
</td>
<td>
#Html.DisplayFor(modelItem => item.Band)
</td>
<td>
#Html.DisplayFor(modelItem => item.Modulation)
</td>
<td>
#Html.DisplayFor(modelItem => item.RST)
</td>
<td>
#Html.DisplayFor(modelItem => item.Comment)
</td>
<td>
#Html.DisplayFor(modelItem => item.QLS)
</td>
<td>
#using (Html.BeginForm("Edit", "Connection", FormMethod.Get))
{
<input type="submit" value="Edit" formaction="/Connection/Edit/#item.IdConnection" />
}
</td>
<td>
#using (Html.BeginForm("DeleteItem", "Connection", FormMethod.Post))
{
<input type="submit" value="Delete" formaction="/Connection/DeleteItem/#item.IdConnection" />
}
</td>
</tr>
}
</table>
#using (Html.BeginForm("DeleteMultiple", "Connection", FormMethod.Post))
{
<input type="submit" value="Delete Selected"/>
}
I selected items in grid (checked checkbox) and in method :public ActionResult DeleteMultiple(System.Guid[] deleteInputs) array deleteInputs always is null.
Your html.beginform pointing to "DeleteMultiple" has nothing being passed to it! You need to either restructure your view to include the inputs in your form or, since this is a delete function, make an ajax call to your controller action and send in the values of the selected check boxes.
I am trying to display a complex viewmodel in a view, everything works fine except one dropdownlist. I am displaying a list of enum, and set the selected object as it should be. But for an unknown reason, its not showing the selected value. I have tried many times with but failed -- please help me. The code is given below -
Enum:
public enum CommentStatus
{
NoStatus = 0,
Resolved = 1,
NotApplicable = 2
}
Model:
public class CheckComments
{
public string EntryId { get; set; }
public int Serial { get; set; }
public string Comment { get; set; }
public DateTime CommentDate { get; set; }
public CommentStatus CommentStatus { get; set; }
}
public class CheckDataViewModel
{
public string EntryId { get; set; }
public string CheckId { get; set; }
public decimal Value { get; set; }
public List<CheckComments> CheckCommentsList { get; set; }
public List<string> CommentStatusList { get; set; }
}
Controller:
public class CheckDataController : Controller
{
public ActionResult GetEnteredCheckData(string entryId)
{
var checkDataViewModel = new CheckDataViewModel();
var checkData = new CheckDataManager().GetCheckData(entryId);
checkDataViewModel.EntryId = checkData.EntryId;
checkDataViewModel.CheckId = checkData.CheckId;
checkDataViewModel.Value = checkData.Value;
checkDataViewModel.CheckCommentsList = checkData.Comments;
checkDataViewModel.CommentStatusList = Enum.GetNames(typeof (CommentStatus)).ToList();
return View("EnteredCheckData", checkDataViewModel);
}
}
View:
#model PDCA.Web.Models.CheckDataViewModel
#{
ViewBag.Title = "EnteredCheckData";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<fieldset>
<div class="row">
<div class="span3">
<div class="text-left">#Html.LabelFor(model => model.EntryId)</div>
</div>
<div class="span3">
<div class="text-left">
#Html.DisplayFor(model => model.EntryId)
#Html.HiddenFor(model => model.EntryId)
</div>
</div>
</div>
</fieldset>
<fieldset>
<legend>Check Information</legend>
<div class="row">
<div class="span3">
<div class="text-left">#Html.LabelFor(model => model.CheckId)</div>
</div>
<div class="span3">
<div class="text-left">
#Html.DisplayFor(model => model.Check.CheckId)
#Html.HiddenFor(model => model.Check.CheckId)
</div>
</div>
</div>
</fieldset>
<fieldset>
<legend>Comments</legend>
<div>
<table class="table table-bordered">
<tr>
<th>
Serial
</th>
<th>
Comment
</th>
<th>
Date
</th>
<th>
Status
</th>
<th>
</th>
</tr>
#for (int i = 0; i < Model.CheckCommentsList.Count; i++)
{
<tr>
<td>
#Html.DisplayFor(modelItem => Model.CheckCommentsList[i].Serial)
#Html.HiddenFor(modelItem => Model.CheckCommentsList[i].Serial)
</td>
<td>
#Html.DisplayFor(modelItem => Model.CheckCommentsList[i].Comment)
#Html.HiddenFor(modelItem => Model.CheckCommentsList[i].Comment)
</td>
<td>
#Html.DisplayFor(modelItem => Model.CheckCommentsList[i].CommentDate)
#Html.HiddenFor(modelItem => Model.CheckCommentsList[i].CommentDate)
</td>
<td>
#Html.DropDownListFor(modelItem => Model.CheckCommentsList[i].CommentStatus, new SelectList(Model.CommentStatusList, Model.CheckCommentsList[i].CommentStatus))
#Html.ValidationMessageFor(model => Model.CheckCommentsList[i].CommentStatus)
</td>
<td>
#Html.ActionLink("Update Status", "UpdateCommentStatus", new { Model.EntryId, Model.CheckCommentsList[i].Serial, Model.Check.CheckTitle, Model.Check.CheckId })|
#Html.ActionLink("Delete", "DeleteComment", new { Model.CheckCommentsList[i].Serial, Model.EntryId })
</td>
</tr>
}
</table>
</div>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
The code is too lengthy but I think this will help you to understand my mistakes. Thanks in advance friends.
Solution:
#Html.DropDownListFor(modelItem => Model.CheckCommentsList[i].CommentStatus, new SelectList(Model.CommentStatusList, Model.CheckCommentsList[i].CommentStatus.ToString()))
I am trying to make a stock take application, My view loads all my stock with one editor.
My controller is not getting any of the data from the view?
I want to be able to edit all my stock at the same time?
How can I do this
Model Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FlatSystem.Models
{
public class Stock
{
public int ID { get; set; }
public string Item_Name { get; set; }
public int Actual_Stock { get; set; }
public int Wanted_Stock { get; set; }
}
}
View Code
#model IEnumerable<FlatSystem.Models.Stock>
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<div class="sidemenu">
<div class="sidemenu-heading">
ReStock
</div>
<div class="div-body">
<table>
<tr>
<th>
Item Name
</th>
<th>
Wanted Stock
</th>
<th>
Stock On Hand
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Item_Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Wanted_Stock)
</td>
<td>
<div class="editor-field">
#Html.EditorFor(modelItem => item.Actual_Stock)
#Html.ValidationMessageFor(modelItem => item.Actual_Stock)
</div>
</td>
#Html.HiddenFor(modelItem => item.ID)
</tr>
}
</table>
</div>
</div>
<input type="submit" value="Submit" />
}
Controller Code
[HttpPost]
public ActionResult ReStock(List<Stock> stock)
{
foreach (var item in stock)
{
if (ModelState.IsValid)
{
GR.InsertOrUpdate(item);
}
}
GR.Save();
return RedirectToAction("Restock");
}
It's hard to answer your question without model class, but idea is that your edit inputs must contain index in name attribute.
Something like this:
#for(int i = 0: i < Model.Count(); i++)
{
<tr>
<td>
#Html.DisplayFor(modelItem => Model[i].Item_Name)
</td>
<td>
#Html.DisplayFor(modelItem => Model[i].Wanted_Stock)
</td>
<td>
<div class="editor-field">
#Html.EditorFor(modelItem => Model[i].Actual_Stock)
#Html.ValidationMessageFor(modelItem => Model[i].Actual_Stock)
</div>
</td>
#Html.HiddenFor(modelItem => Model[i].ID)
</tr>
}
Added:
Sorry, thanks to Darin Dimitrov, you can't access IEnumerable by index, use List or Array.
You could use editor templates. I would recommend you to first read the following article in order to understand why your code doesn't correctly bind the collection. Once you understand that you could do the following:
#model IEnumerable<FlatSystem.Models.Stock>
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<div class="sidemenu">
<div class="sidemenu-heading">
ReStock
</div>
<div class="div-body">
<table>
<thead>
<tr>
<th>Item Name</th>
<th>Wanted Stock</th>
<th>Stock On Hand</th>
<th></th>
</tr>
</thead>
<tbody>
#Html.EditorForModel()
</tbody>
</div>
</div>
<input type="submit" value="Submit" />
}
and now define a custom editor template for the Stock type which will automatically be rendered for each element of the collection (~/Views/Shared/EditorTemplates/Stock.cshtml) - the name and location of the editor template is important as it works by convention:
#model FlatSystem.Models.Stock
<tr>
<td>
#Html.DisplayFor(x => x.Item_Name)
</td>
<td>
#Html.DisplayFor(x => x.Wanted_Stock)
</td>
<td>
<div class="editor-field">
#Html.EditorFor(x => x.Actual_Stock)
#Html.ValidationMessageFor(x => x.Actual_Stock)
</div>
</td>
#Html.HiddenFor(x => x.ID)
</tr>
Remark: You might also want to include the Wanted_Stock and Item_Name as hidden fields along with the ID in the editor template in order for their values to be sent to the server, because you don't have a corresponding input field for them.
I am developing MVC application and using razor syntax.
I have used model first method.
I have two entities, Customer and Lead. Lead is inherited from Customer.
When IsActive property is true then Customer treated as a Lead, otherwise it will be a customer.
Please check edmx file image.
When I genrated DB from model , I get the two tables Customer Table and Customer_Lead
table.
This is Customer table.
and This is Customer_Lead Table.
Now the problem is when I run index view of lead entity , Its give the error.
The model item passed into the dictionary is of type
PagedList.PagedList1[CRMEntities.Customer], but this dictionary
requires a model item of type
PagedList.IPagedList1[CRMEntities.Lead].
I am getting confused abt how to write a view for the entity which is inherited from other entity and how to access the data from these two tables while working with inherited entity.
Index View code is as below...
#model PagedList.IPagedList<CRMEntities.Lead>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Status
</th>
<th>
IsQualified
</th>
<th>
Name
</th>
<th>
Address
</th>
<th>
OfficePhone1
</th>
<th>
Website
</th>
<th>
Email2
</th>
<th>
Email1
</th>
<th>
FaxNo
</th>
<th>
Remark
</th>
<th>
Rating
</th>
<th>
BusinessType
</th>
<th>
IsActive
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Status)
</td>
<td>
#Html.DisplayFor(modelItem => item.IsQualified)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Address)
</td>
<td>
#Html.DisplayFor(modelItem => item.OfficePhone1)
</td>
<td>
#Html.DisplayFor(modelItem => item.Website)
</td>
<td>
#Html.DisplayFor(modelItem => item.Email2)
</td>
<td>
#Html.DisplayFor(modelItem => item.Email1)
</td>
<td>
#Html.DisplayFor(modelItem => item.FaxNo)
</td>
<td>
#Html.DisplayFor(modelItem => item.Remark)
</td>
<td>
#Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
#Html.DisplayFor(modelItem => item.BusinessType)
</td>
<td>
#Html.DisplayFor(modelItem => item.IsActive)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
#Html.ActionLink("Details", "Details", new { id=item.Id }) |
#Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
I have following code in index...
public ViewResult Index()
{
var LeadList = (from c in db.Customers
where (c.IsActive == true)
orderby (c.Id)
select c);
//What should I return ?
}
Your view looks fine, but you're trying to pass a list of Customers to it. Try getting a list of Lead customers from context.
public ActionResult Index()
{
...
var leadList = db.Customers.OfType<CRMEntities.Lead>().Where(c => c.IsActive).OrderBy(c => c.Id);
return View(leadList);
}
i have the following Main view inside my asp.net mvc web application:-
<table>
<tr>
<th>
Lab Test
</th>
<th>
Result
</th>
<th>
Date Taken
</th>
<th>
Comment
</th>
<th>
</th>
#for (int item = 0; item < 10; item++)
{
using (Ajax.BeginForm("CreateAll", "VisitLabResult", new AjaxOptions
{
HttpMethod = "Post",
UpdateTargetId = item.ToString(),
InsertionMode = InsertionMode.Replace,
OnSuccess = string.Format(
"disableform({0})",
Json.Encode(item)),
}))
{ <tr id = "#item">
#Html.Partial("_create",Model)
</tr>
}
} </table>
which renders the following _create partial view:-
#section scripts{
}
<td>
#Html.DropDownList("LabTestID", String.Empty)
#Html.ValidationMessageFor(model => model.LabTestID)
</td>
<td>
#Html.EditorFor(model => model.Result)
#Html.ValidationMessageFor(model => model.Result)
</td>
<td>
#Html.EditorFor(model => model.DateTaken)
#Html.ValidationMessageFor(model => model.DateTaken)
</td>
<td>
#Html.EditorFor(model => model.Comment)
#Html.ValidationMessageFor(model => model.Comment)
</td>
<td>
<input type= "hidden" name = "visitid" value = "#ViewBag.visitid" />
<input type="submit" value="Create" />
</td>
currently all the data annotation client side validation such as Required will not show up whe using firefox or chrome but they will work correctly using IE9 ,, so what might be the problem.
BR
I found a typo in this code block. You added a komma after you Json.Encode(item). If this is the last item, you cannot add a komma.
<table>
<tr>
<th>
Lab Test
</th>
<th>
Result
</th>
<th>
Date Taken
</th>
<th>
Comment
</th>
<th>
</th>
#for (int item = 0; item < 10; item++)
{
using (Ajax.BeginForm("CreateAll", "VisitLabResult", new AjaxOptions{
HttpMethod = "Post",
UpdateTargetId = item.ToString(),
InsertionMode = InsertionMode.Replace,
OnSuccess = string.Format("disableform({0})", Json.Encode(item))})
){
<tr id = "#item">
#Html.Partial("_create",Model)
</tr>
}
}
</table>
Hope this will work. Let me know.