I'm using ASP.NET MVC4 C# and I'm facing a problem with displaying an array in my View:
I'd like to display a number of error messages from my controller to the view, but so far, the content of only 1 array is displayed...
Only the data in "ViewData["mededelingen"] is displayed. The data in ViewData["warnings"] is not displayed, despite the same code.
Below is the code of the view:
When I debugged, I have noticed that the ViewData["warnings"] is not empty, as shown in the next screenshot:
However, it does not display the contents on my HTML page.
Below is the output:
As you can see, only the items in red (mededelingen) are displayed, the items in yellow (warnings) are not.
Below is the code of the controller:
Obviously I'm doing something wrong, but I can't figure out what exactly...
Any help?
Thanks in advance!!
DisplayName gets the display attribute of the model property represented by the string that's passed to it. Since your string is just a sentence, that doesn't make sense. Why are you using DisplayName at all?
Just do:
#foreach (var counter2 in (ViewData["warnings"] as List<string>))
{
<td>#counter2</td>
}
Related
This is my first dropdown list in my view.:
And this is my controller:
My problem is that whenever I click the available dropdown in my first dropdown list it should populate the data available on the one that I have clicked on the second dropdown list. But It has an error it will not proceed to the next line which the JavaSerializer. How can I fix my code like it will work. I don't know where is the problem is, whether in the ajax? or in my controller?
Please change the select as jQuery("#availability option:selected").val();
- Change the ajax type as GET method in client side
- JavascriptSerializer not required
- remove [httppost] tag from code behind
- check where any error is there in browser console.
and change $('#availability').push("" + this.name+ "");
I have a dropdownlist inside a form and a div. I want that, when i select a value from the dropdownlist, it shows in the div.
I've succeeded in this, but the problem is that the following selections ,after first one, are deleting previous results from the div.
public ActionResult SelectStudWhoWantReport(int StudentId = 0)
{
IEnumerable<SelectListItem> items = db.Students.Select(c => new SelectListItem
{
Value = SqlFunctions.StringConvert((double)c.StudentID).Trim(),
Text = c.StudentName
});
ViewBag.StudentId = items;
Student WhomakeReport = db.Students.Find(StudentId);
ViewBag.Name = WhomakeReport.StudentName;
return View();
}
viewbag is the value that apear in div, When I select that how can I append the value of the second select with the first select to show the two in the div.
I think you are using a bad approach to solve your problem.
You are getting the result of the selection on your controller class, through a form submission I supose, and later send it to your View.
The problem is that when you call the controller your page is reloaded, thus you loose the previous info. There are different ways in which you'd be able to store previous results and build them on the View.
However, I think it's better that I explain you a more correct approach to solve this problem.
You should have a dropdownlist component on your View, which you load through your controller. I supose you already have this.
When the selection is made, instead of submitting the form, make an AJAX call to a method in the server which makes what you're currently doing in your controller (get the result depending on the selected id) and, instead of returning an ActionResult, just return the value you want.
When the AJAX callback returns, you'll have the result on your client code, so you just have to append it after previous results.
This approach not only is better from a MVC pattern point of view, it's also easier to implement and will allow your users to select elements in the dropdownlist and see the results without having to reload the whole page.
I am making a web application with ASP MVC4 application. I am also using the TwitterBootstrapMVC api. I am trying to make a view where I have a drop down list at the top along with a text-box. Below, is a table of people. The drop down list will contain the column headers of the table. Once the user selects a column in the drop down list, they can then type into the text-box to narrow the results in the list of items in the table below.
For example. The table has many people in it. The user then selects "Last Name" in the drop down list. He then proceeds to type Smith into the text-box. As he types every character, the table's results are narrowed by only displaying people with a "s" in their last name. When the "m" is typed into the text-box, the list of people are narrowed to only show people with "sm" in their last name etc.
I know how to make the table, text-box, etc. I do not know how to make the results in the table change as I type in the text-box without reloading the page.
You can use jQuery's ajax and an action method that returns a partial view containing the searched data and your html table strucutre. Here's a short sample:
public ActionResult Search(string searchTerm){
var model = db.Users.Where(u => u.LastName.contains(searchTerm));
return PartialView("_Students", model);
}
Script
$("#textBoxId").on("keyup change", function() {
var searchTerm = $(this).val();
$.get("/Controller/Search", { searchTerm: searchTerm }, function(data) {
$("#tableId").html(data);
});
});
You just need to add another parameter to your action method for checking what was selected in the dropdown.
Read this for more: https://api.jquery.com/jQuery.get/
What you're trying to implement is an autocomplete textbox. jQuery has a nice one. It is fairly simple to implement.
Or you can try out the ASP.NET AJAX Control toolkit. I haven't tried this one myself, so I'm not sure how simple/friendly it is.
In my asp.net MVC site, i have a view with a empty grid and some textboxes, when the user fills those textboxes i need to get those values and make a new row for the grid.
How can i do that?
Thanks
ps(using Razor)
Take a look at the online GridView Filtering demo.
If the current "filter string" is empty (see the "Controller -> Filtering" action method; the "startsWith" parameter), do not initialize model.
If not empty, initialize the required model.
I'm currently working on a large project which has a Timesheet class containing a list of TimesheetEntries. There is an edit page for this that looks like:
<!-- Other timesheet properties -->
#Html.EditorFor(model => model.TimesheetEntries)
This works great, I get a row for each timesheet entry. When I save the timesheet (POST to the server) a custom model binder strips out empty rows before passing a Timesheet to the controller, in other words the Timesheet can contain fewer rows than the POST data.
If there is a validation error I redisplay the edit page and this is where the problem arises.
If model.TimesheetEntries contains n records Html.EditorFor() reproduces the first n rows from the POST data (including the empty rows) rather than creating HTML for the data I pass it! This means I lose a number of rows at the bottom of the table which is clearly unwanted.
Can anyone explain why this is happening or even better tell me how to get EditorFor to work as I expect?
Here's a picture to describe the process:
You may want to take a look at the answer provided in this post Asp:net MVC 3: #Html.EditorFor a subcollection of my model in a template? It may help.
Turns out Html.EditorFor looks in ModelState before using the values you pass it... So to get it to pick up the values I passed it I needed to clear the relevant ones out of ModelState first, ugh.
This question/answer covers it in more detail.