I have this on my view and this works well, persisting the country selected:
<%: Html.DropDownListFor(model => model.CountryId, Model.CountryList ,"Select Country") %>
However when I create an editor template for this, the country selected is not persisted
So, I change my current view to this:
<%: Html.EditorFor(model => model.CountryId,new { countries = Model.CountryList}) %>
Then I create my editor template like this:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.Int64?>" %>
<%= Html.DropDownList(
String.Empty /* */,
(SelectList)ViewData["countries"],
Model
)
%>
I would try something like this:
Code in parent view:
<% Html.RenderPartial("YourPartialView", Model) %>
Code in editor view:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ModelFromParent>" %>
<%= Html.DropDownList(
String.Empty /* */,
new SelectList(Model.Countries,"Id", "Title"),
"Select Country"
)
%>
I didn't compile this. But you get the general idea.
Related
I have an ASP.NET MVC2 application. On one of my pages I have a textbox defined in the following manner:
<%: Html.TextBoxFor(model => model.PostCode) %>
Which is working perfectly.
However, for specific countries (model.Country) I do not want to show this TextBox.
What is the best way of implementing this?
This is an .aspx file, not .cshtml
Thanks
Create a property in your model class, and try this:
#if (!Model.IsSpecificCountry) {
#Html.TextBoxFor(model => model.PostCode)
}
Update:
<%if (!Model.IsSpecificCountry) { %>
<%= Html.TextBoxFor(model => model.PostCode) %>
<% } %>
I have another simple question for all you mvc gurus.
I have a partial view with the following definition.
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IList<whoozit.Models.PictureModel>>" %>
How do I access the data that has been assigned to the view?
You could do something like:
<% foreach (whoozit.Models.PictureModel picture in Model)
{ %>
<%: picture.property1 %><br />
<%: picture.property2 %><br />
etc...
<% } %>
Code:
<% foreach (var item in Model) { %>
<td>
<%= Html.Encode(item.BirthDate) %>
</td>
<% } %>
display this: 8/24/2009 12:00:00 AM but I need only date (8/24/2009). It is possible to do without any formating in controller action
There are a couple of ways to go about it. If you're using MVC 2, you could use a DisplayTemplate. Just put a DateTime.ascx file in the folder called /Views/Shared/DisplayTemplates and the line of code it would have in it is:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %>
<%: Model.HasValue ? Model.Value.ToShortDateString() : string.Empty %>
(Note: if you use the <%: syntax then you don't need the Html.Encode() because <%: does the HTML encoding for you - but that's only if you're using VS2010. If you're using MVC 2 with VS2008, then stick with the Html.Encode() for this part) Then in your view you would simply do this:
<%: Html.DisplayFor(m => m.BirthDate) %>
That will change the format to only have the Date for all DateTime's in your application. Of course you could put that directly in the view as well. But the DisplayTemplate will change it for all other DateTime's as well and you won't have to concern yourself with it in the view since it happens for your automatically.
If BirthDate is string datatype
<%= Html.Encode(!String.IsNullOrEmpty(item.BirthDate) ?
Convert.ToDateTime(item.BirthDate).ToShortDateString() : item.BirthDate) %>
If BirthDate is datetime datatype
<%=Html.Encode(String.Format("{0:MM/dd/yyyy}", item.BirthDate))%>
I would say that formatting is a responsibility of the view, not the controller, so format the output like this:
<% foreach (var item in Model) { %>
<td>
<%= Html.Encode(item.BirthDate.ToShortDateString() %>
</td>
<% } %>
If item.BirthDate is type of DateTime you can use ToShortDateString() method:
item.BirthDate.ToShortDateString();
<% foreach (var item in Model) { %>
<td>
<%= Html.Encode(item.BirthDate.ToString("MM/dd/yyyy")) %>
</td>
<% } %>
In the new version of MVC we have been given display and editor templates. I was wondering how to make the best possible use of these. I was at first thinking in the line of creating one for a collection and one for an item in the case I want to display either as a list or single but it does not really make sense to me. I'll show you what I mean and please comment if this is wrong. I have trouble figuring it out myself.
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<DisplayPhoneModel>>" %>
<% foreach (var item in Model){%>
<%= Html.DisplayFor(x => item) %>
<%}%>
So in my tiny reality that would for every item in the model search for a display template for DisplayPhoneModel. That display template could then simply look like.
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DisplayPhoneModel>" %>
<%= Html.LabelFor(x => x.PhoneType) %> <%= Html.LabelFor(x => x.PhoneNumber) %>
So, question is if there is a better way to handle this? Am I at all on the right path?
No need to foreach manually:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<DisplayPhoneModel>>" %>
<%= Html.DisplayForModel() %>
And the display template:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DisplayPhoneModel>" %>
<%= Html.LabelFor(x => x.PhoneType) %>
<%= Html.LabelFor(x => x.PhoneNumber) %>
I am using VS 2008 MVC.
I developed a controller.
Form controller i fetch the data by using LinqToSql.
& i am retuning a list of that data.
e.g. return View(Students.Tolist());
now i want to display the list using "foreach" loop in view.
so how do i achieve it?
You return your Student list in the Controller Action as the Model for the View, so make sure your View is strongly typed. Your View should have this at the top:
Inherits="System.Web.Mvc.ViewPage<List<Your.Namespace.Student>>"
Then you can iterate over that list in the View like this:
<% foreach(var student in Model)
{ %>
<div class="student">
<%= student.Name %>
<%= student.Age %>
</div>
<% } %>
This is if you use the MVC RC or newer.
You should have a Student class to hold the records outputted by LINQ to SQL. Anonymous types don't work well in this scenario.
<% foreach (var item in (IEnumerable<Student>)ViewData.Model) { %>
<div class="student">
Name: <%= item.Name %> <br />
GPA: <%= item.GPA %>
</div>
<% } %>