I have the code below which loads on edit mode. but when i get the infor back to server it dosent return any rows any ideas why. the method does go into the post method below. this will be a update method in edit mode.
#model List<SubjectSuggestionModel>
#using Telerik.Sitefinity.UI.MVC
#using Telerik.Sitefinity.UI.MVC.Helpers
<script type="text/javascript">
$(document).ready(function () {
$("#grid").kendoGrid({
sortable: true,
pageable: true,
dataSource: { pageSize: 20 }
});
});
</script>
<div class="container">
<fieldset>
<table id="grid">
<thead>
<tr>
<th data-sortable="false">
Subject ID
</th>
<th data-sortable="false">
Suggestion order
</th>
<th data-field="QuestionText">
Suggested subject
</th>
</th>
</tr>
</thead>
#using (Html.BeginFormSitefinity("editSubjectSuggestions", "editSubjectSuggestions"))
{
#Html.ValidationSummary(true)
<tbody>
#foreach (var i in Model)
{
<tr>
<td>#Html.TextBoxFor(model => i.subject_id, new { #readonly = "readonly" })
</td>
<td>#Html.TextBoxFor(model => i.suggestion_order)
</td>
<td>#Html.TextBoxFor(model => i.suggested_subject_id)
</td>
</tr>
}
</tbody>
}
</table>
<input type="submit" value="confirm" runat="server" />
</fieldset>
</div>
Action methods
[HttpPost]
public ActionResult editSubjectSuggestions(List<SubjectSuggestionModel> models)
{
return View("subjectSuggestion", prsubList.getSubjectSuggestionsList(
Convert.ToInt32(Session["subjectID"].ToString())));
}
Using a foreachloop generates duplicate id attributes (invalid html) and duplicate name attributes without indexers which cannot be bound to a collection. You must use a for loop or a custom EditorTemplate for the type.
Using a for loop
<tbody>
#for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>#Html.TextBoxFor(m => m[i].subject_id, new { #readonly = "readonly" })</td>
<td>#Html.TextBoxFor(m => m[i].suggestion_order)</td>
<td>#Html.TextBoxFor(m => m[i].suggested_subject_id)</td>
</tr>
}
</tbody>
Using an EditorTemplate
In /Views/Shared/EditorTemplates/SubjectSuggestionModel.cshtml
#model SubjectSuggestionModel
<tr>
<td>#Html.TextBoxFor(m => m.subject_id, new { #readonly = "readonly" })</td>
<td>#Html.TextBoxFor(m => m.suggestion_order)</td>
<td>#Html.TextBoxFor(m => m.suggested_subject_id)</td>
</tr>
and in the main view
<tbody>
#Html.EditorFor(m => m)
</tbody>
Related
I have a view like this:
#model IEnumerable<Namespace.MyClass>
#using Newtonsoft.Json;
<form asp-action="Update">
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.Product)
</th>
<th>
#Html.DisplayNameFor(model => model.IsAvailable)
</th>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Product)
</td>
<td>
#Html.EditorFor(modelItem => item.IsAvailable)
</td>
</tr>
}
</tbody>
</table>
<div>
<a href=#Url.Action("SaveChanges", "Product", new {productList = JsonConvert.SerializeObject(Model) })> Save changes</a>
</div>
</form>
where SaveChanges looks like this:
[HttpGet] // Perhaps it should be HttpPost here, but if I change it to that nothing happens when I run the program after clicking the "Save changes" link
public ActionResult SaveChanges(string productList)
{
List<MyClass> products = JsonConvert.DeserializeObject<List<MyClass>>(productList);
// Here I continue with SqlCommand to later make an UPDATE to a database changing the value of IsAvailable which is the only adjustable value in my view
}
My issue is that when I run the program and I change IsAvailable (which is a bool) from false to true in my view and press "Save Changes", the model does not change, i.e. productList still has the value False for IsAvailable for that particular product.
Does anybody have an idea as to why? From my understanding if I have a and
Model information is not updated when you send the model to the action with the link. And the same initial amount is sent.
The changes you make to the model fields are only in the html controls and have nothing to do with the model. The model is updated when you post the form information.
I submitted the form to jQuery in the following code
in view
#model List<Namespace.MyClass>
#using (Html.BeginForm("SaveChanges", "Product", FormMethod.Post))
{
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.FirstOrDefault().Product)
</th>
<th>
#Html.DisplayNameFor(model => model.FirstOrDefault().IsAvailable)
</th>
</thead>
<tbody>
#for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>
#Html.DisplayFor(modelItem => Model[i].Product)
</td>
<td>
#Html.EditorFor(modelItem => Model[i].IsAvailable)
</td>
</tr>
}
</tbody>
</table>
<div>
#Html.ActionLink("Save changes", "", null, new { #id = "submit"})
</div>
}
#section scripts{
<script type="text/javascript">
$("#submit").click(function () {
document.forms[0].submit();
return false;
});
</script>
}
action in controller
[HttpPost] // Perhaps it should be HttpPost here, but if I change it to that nothing happens when I run the program after clicking the "Save changes" link
public ActionResult SaveChanges(IEnumerable<MyClass> products)
{
// Here I continue with SqlCommand to later make an UPDATE to a database changing the value of IsAvailable which is the only adjustable value in my view
}
Sorry I didn't really know how to title this.
I have a webpage that displays announcements. What I cant seemed to figure out is whether there are any announcements, if there are display saying "There are no announcements".
I've tried stuff like:
if(db.Announcements.toArray().length == 0){
return View(null);
}
but that doesn't work. Where do I deal with things like these? View/Controller?
View:
#model IEnumerable<Remake.Models.Announcement>
#{
ViewBag.Title = "Announcements";
}
<h2>Announcements</h2>
#if (User.Identity.IsAuthenticated)
{ <p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
<b> #Html.DisplayNameFor(model => model.Title)</b>
</th>
<th>
#Html.DisplayNameFor(model => model.Content)
</th>
<th width="10%">
#Html.DisplayName("Date")
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
<b> #Html.DisplayFor(modelItem => item.Title)</b>
</td>
<td>
#Html.DisplayFor(modelItem => item.Content)
</td>
<td>
<b>#Html.DisplayFor(modelItem => item.PostDate)</b>
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.AnnouncementId }) |
#Html.ActionLink("Comments", "Details", new { id = item.AnnouncementId }) |
#Html.ActionLink("Delete", "Delete", new { id = item.AnnouncementId })
</td>
</tr>
}
</table>
}
else
{
<p>Please sign in to create an Announcement</p>
}
Controller:
// GET: Announcements
public ActionResult Index()
{
return View(db.Announcements.ToList());
}
Since your model is defined as IEnumerable<Announcement>, you can simply use Any() to check whether it's empty or not:
#if (Model.Any())
{
// show announcements
foreach (var item in Model)
{
// ...
}
}
else
{
// show message when empty
<p>No announcements</p>
}
See MSDN
I'm trying to do something that seems to be a little bit tricky and since i'm a complete beginner in ASP.NET, JQuery and so on, i'm here to have some advices.
I've rceated a formular that is displayed that way :
The code looks like that :
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>
Room
</th>
<th>
Room name
</th>
<th>
User
</th>
</tr>
</thead>
<tbody>
#for (int i = 0; i < Model._listPerson.Count(); i++) {
<tr>
<td>
#Html.EditorFor(m => m._listPerson[i].myValue.isSelected)
</td>
<td>
#Html.DisplayFor(m => m._listPerson[i].myValue.name)
#Html.HiddenFor(m => m._listPerson[i].myValue.name)
#Html.HiddenFor(m => m._listPerson[i].myKey)
</td>
<td>
<!--Obtain the KeyValuePair<int, List<PersonModel>> that correspond to the index.-->
#Html.DropDownListFor(m => m.selectedPerson[i], new SelectList(Model._list[i].myValue, "id", "name"))
</td>
</tr>
}
</tbody>
</table>
<p>Assign all selected rooms to user #Html.DropDownListFor(m => m.useless, new SelectList(Model._list[0].myValue, "id", "name"))</p>
So the aim of what I want to do is to update all the fields "assigned user" to the name of the user that is selected at the bottom of the table. How to do that easily ?
Thanks !
Add a class to your DropDownLists in the loop like this:
#Html.DropDownListFor(m => m.selectedPerson[i], new SelectList(Model._list[i].myValue, "id", "name"), new { #class = "ddl" })
Then, add this script to your page:
<script type="text/javascript">
$(document).ready(function () {
$('#useless').change(function () {
var selected = $(this).val();
$(".ddl").val(selected);
});
});
</script>
It's ok now, thanks to ataravati. I had to change the solution a little bit. Here is the javascript :
<script type="text/javascript">
$(document).ready(function () {
$('#useless').change(function () {
var selected = $(this).val();
$(':checkbox').each(function () {
var tag = $(this).attr('id');
tag = tag.replace(/^select/, "");
tag = ".ddl" + tag;
console.log("tag : " + tag);
if ($(this).attr('checked'))
$(tag).val(selected);
});
});
});
</script>
And here is the new version of the code that display the page :
<tbody>
#for (int i = 0; i < Model._listPerson.Count(); i++) {
<tr>
<td>
#Html.CheckBoxFor(m => m._listPerson[i].myValue.isSelected, new { id = "select"+i })
</td>
<td>
#Html.DisplayFor(m => m._listPerson[i].myValue.name)
#Html.HiddenFor(m => m._listPerson[i].myValue.name)
#Html.HiddenFor(m => m._listPerson[i].myKey)
</td>
<td>
#Html.DropDownListFor(m => m.selectedPerson[i], new SelectList(Model._list[i].myValue, "id", "name"), new { #class = "ddl"+i })
</td>
</tr>
}
</tbody>
And now, it's working fine ! Thanks !
System.Web.Mvc.AjaxHelper does not contain a definition for 'BeginForm i am getting this error in my mvc3 project please help me to resolve this
here is my code:
#{
ViewBag.Title = "Register";
Layout = "~/Views/Shared/_MyLayout.cshtml";
}
#model FoodMart.Models.AccountModel
#using (Ajax.BeginForm("Register", new AjaxOptions { UpdateTargetId = "result" }))
{
<div id="result" style="width:800px">
<fieldset style="width:100%">
<legend><span style="font-size:larger; color:Green; font-family:Arial">Register Here</span></legend>
<table style="width:100%">
<tr>
<td>#Html.LabelFor(m => m.users.FirstName,"Enter First Name")</td>
<td>#Html.TextBoxFor(m => m.users.FirstName, new { style = "width:150px" })</td>
<td><span style="font-size:medium; color:Green;">#Html.ValidationMessageFor(m=>m.users.FirstName)</span></td>
</tr>
<tr>
<td>#Html.LabelFor(m => m.users.Landline)</td>
<td>#Html.TextBoxFor(m => m.users.Landline, new { style = "width:150px" })</td>
</tr>
<tr>
<td><input type="submit" value="Register" /></td>
<td></td>
</tr>
</table>
</fieldset>
</div>
}
AjaxHelper.BeginForm is an extension method defined in the namespace System.Web.Mvc.Ajax.
Do you have this namespace defined in the web.config in your Views folder?
Have you tried inserting this instruction at the top of your view ?
#using System.Web.Mvc.Ajax
I'm trying to create a way so that when clicking the submit button or a link, do the submission
and remain in the same modal dialog.
My program has two forms within the same dialog, one is not shown until the first is submited and i would like that on submiting the first form load, within the same dialog.
For the load dialog i have this code:
function loadDialog(tag, event, target) {
event.preventDefault();
var $loading = $('<img src="../../Content/assets/images/nivo-loader.gif" alt="loading" class="ui-loading-icon">');
var $url = $(tag).attr('href');
var $title = $(tag).attr('title');
var $dialog = $('<div></div>');
$dialog.empty();
$dialog
.append($loading)
.load($url)
.dialog({
autoOpen: false
, title: $title
, width: 600
, modal: true
, minHeight: 200
, show: 'fade'
, hide: 'fade'
});
});
Then in the Create.cshtml:
#model Mvcproject.Models.MyModel
<script language="javascript" type="text/javascript">
$(function () {
$("input[type=submit]")
.button(
);
$('a.modalDlg2').live("click", function (event) { loadDialog('#modalDlg', event, '#Receipt Create'); });
/*$('input.Submit').live("click", function (event) {
e.preventDefault();
loadDialog('#modalDlg', event, '#Receipt Create');
});*/
$(".demo button").button({
icons:
{
primary: 'ui-icon-plusthick'
}
});
});
</script>
<h2>
Add line</h2>
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<fieldset class="createReceipt">
<legend>User login</legend>
#if (ViewBag.Created)
{
<table class="createTable">
<tr>
<td rowspan="4">
<img src="../../Images/no_image.jpg" width="100" height="100" />
</td>
<th colspan="1">
#Html.LabelFor(model => model.User.Name)
</th>
<td colspan="5">
#Html.EditorFor(model => model.User.Name)
#Html.ValidationMessageFor(model => model.User.Name)
</td>
</tr>
<tr>
<th colspan="1">
#Html.LabelFor(model => model.User.Email)
</th>
<td colspan="5">
#Html.EditorFor(model => model.User.Email)
#Html.ValidationMessageFor(model => model.User.Email)
</td>
</tr>
<tr>
<th colspan="1">
#Html.LabelFor(model => model.User.Password)
</th>
<td colspan="5">
#Html.EditorFor(model => model.User.Password)
#Html.ValidationMessageFor(model => model.User.Password)
</td>
</tr>
<!--User picture-->
<tr>
<td>
<input type="file" value="browse" name="Browse" accept="image/gif, image/jpeg, image/jpg" />
</td>
</tr>
</table>
<p>
<input type="submit" class="Submit" value="Create" />
<buttonreceipt style="width: 150px; text-align: left">#Html.ActionLink("Create", "Create")</buttonreceipt>
</p>
}
else
{
<!-- This will be displayed when user data is submited and inserted into database-->
<table class="createTable">
<tr>
<th colspan="1">
#Html.LabelFor(model => model.User.Name)
</th>
<td colspan="5">
#Html.DisplayFor(model => model.User.Name)
</td>
</tr>
<tr>
<th colspan="1">
#Html.LabelFor(model => model.User.Email)
</th>
<td colspan="5">
#Html.DisplayFor(model => model.User.Email)
</td>
</tr>
<tr>
<td colspan="1">
<img src="#Model.User.Picture"/>
</td>
</tr>
</table>
}
</fieldset>
}
#if (!ViewBag.Created)
{
#Html.Partial("_AddUserInformation");
}
ViewBag.Created is either false or true depending on if user data has been inserted with success into database and that functionality is already done.
And _AddUserInformation.cshtml has other information for the user that is trying to register, like address and country...
Hope this explains better my problem.
You can use the jQuery post() function to send data to an address. You'll need to pick out the data from your fields manually using jQuery selectors (shouldn't be difficult) as well as configure a function to receive and treat the data. This link on post should give you what you need to get started. http://api.jquery.com/jQuery.post/