TemplateInfo into Controller - asp.net

how can i get a form field id after submitting the form. im trying like this:
ViewData.TemplateInfo.GetFullHtmlFieldId(logOnParts.Part.UserNameOrEmail)
but no work on controller side. i need to get "Part_UserNameOrEmail" something..
if (String.IsNullOrEmpty(logOnParts.Part.UserNameOrEmail))
{
ModelState.AddModelError("Part_UserNameOrEmail", "error");
TempData["logon-focus-field"] = "Part_UserNameOrEmail";
}
so i will focus the field on view side like this:
$(document).ready(function () {
$('#TempData["logon-focus-field"]').focus();
});

A controller should absolutely not need such information. The generated id is a view specific information. If you need this in a controller this simply means that you have some serious design problem with your application. Unfortunately as you haven't explained your scenario in the question it is difficult to help you solving this problem. So in your controller you could do this:
Expression<Func<MyViewModel, string>> expression = x => x.Part.UserNameOrEmail;
string name = ExpressionHelper.GetExpressionText(expression);
string id = new TemplateInfo().GetFullHtmlFieldId(name);

Related

How to get all user list from orchard

I need to get all of users with their Ids whose are created by orchard admin panel. Now I need all list and I will use it in another service.
Or, I want to get Orchard user table "[Orchard_Users_UserPartRecord]" from entity framework like this code
public IList<UserAttendance> ShowAllUserAttendance()`enter code here`
{
using (UserAttendanceDbContext _db = new UserAttendanceDbContext())
{
return _db.UserAttendances.ToList();
}
}
How can I do it?
Thanks,
kibria
See following.
var userProfiles = _contentManager.Query<UserPart, UserPartRecord>() .List() .Select(user => ((dynamic) user).ProfilePart) .Select(user => new {user.FirstName, user.LastName});
For more details visit following link.
http://orchardprofile.codeplex.com/discussions/271879

Data Format Error in Asp.net

I want to set my Salary field value into $23,000.00 and send that value into my database ,i created below code inside my model class but it has some errors need some one help to fix that errors,I am new to Asp.net MVC4 Please help me to solve that problems.
when i save data it goes to database like that way- $23000
but actually i want to send like that way -$23,000.00
when i view my results in my view page it seems $$23000
But i want that way - $23,000.00
Please help me to solve that problems.
Thanks .
public string Salary
{
get
{
return SalaryFormat;
}
set
{
StringBuilder sb = new StringBuilder();
SalaryFormat = Convert.ToString(sb.AppendFormat("${0: #,###.00}", value));
//SalaryFormat = value;
}
}
Thanks!

Retriving the url of a Hyperlink column which is in a sharepoint list, using Client object model

function IfModuleSucceded(sender, args) {
var existingCount = existingItems.get_count();
var existEnumerator = existingItems.getEnumerator();
while (existEnumerator.moveNext()) {
var currentmodule = existEnumerator.get_current();
var URL = currentmodule.get_item("Request_URL");
alert(URL);
}
}
In this Code i am trying to Retrieve the url of a Hyperlink column which is in a SharePoint list, using Client object model, but i have received an object. How could i get the Url out of this received object ????
when this code is executed, it gives the alert as "[Object Object]".
would anyone help me to sort this out ??
The answer will be alert(url.url) as it's an object.
It will also have a property called description
The Hyperlink field has two properties: Description and Url.
You can access the properties like this: ObjectName.PropertyName
So for your URL object in your example, you can reach the properties like this: URL.Url and URL.Description
I found that Url and Description are case sensitive, so make sure you capitalize where necessary.
This worked great for me.

Using Html.DropDownListFor() with a SelectList

What is the correct way to populate a SelectList when using the Html.DropDownListFor helper in ASP.Net MVC 3?
I basically have a "Create" form which I want to specify the entry values on for a particular field. For example, it is a movie database and I want the ability to enter a Genre, but I don't want users to be able to enter what they like, so I assume a SelectList to be the best answer?
A way that works is to create a new class with a static method to return the SelectList:
public class Genres
{
public static List<string> GetGenres()
{
List<string> myGenres = new List<string>();
myGenres.Add("Comedy");
myGenres.Add("Romantic Comedy");
myGenres.Add("Sci-Fi");
myGenres.Add("Horror");
return myGenres;
}
}
Which can then be used like this:
<div class="editor-field">
#Html.DropDownListFor(model => model.Genre, new SelectList(OLL.Models.Genres.GetGenres()))
</div>
It just doesn't seem like the right/easiest way to do it? I think I'm missing something obvious here...
Thanks for any assistance you can provide.
This is a simple selectList style I use for drop downs all the time, hopefully you find this helpeful.
ViewBag.AlbumListDD = from c in db.Query<DatabaseEntities.VenuePhotoAlbum>("WHERE VenueId = #0", VenueId)
select new SelectListItem
{
Text = c.Name,
Value = c.Id.ToString()
};
Inside my view:
#Html.DropDownList("AlbumId", (IEnumerable<SelectListItem>)ViewBag.AlbumListDD, new { id = "ChangeAlbum" })
Hopefully you can find this useful as this is a database route to go. I typically like to have all of that sort of information in a database and that way users can dynamically add/remove and change these dropdown items at will. There is a bit of SQL work behind the scenes required when removing a type but it has proven to be very successful and fast for me at this point.
var og = (from s in DB.Products
select new SelectListItem
{
Text = s.ProductName,
Value = s.ProductID.ToString()
});
ViewBag.lstProducts = new SelectList(getproductname, "ProductID", "ProductName");
<p>
#Html.DropDownList("AlbumId", (IEnumerable<SelectListItem>)ViewBag.lstProducts, new { #id = "drp" });
</p>

avoiding the use of the viewdata enumerable object in controller - reg

I have 2 points for today
I. I have a controller in which i have a public static method for getting the details for a checkbox like
public static List<country> GetCountryLists()
{
List<country> countries = new List<country>();
country _country = new country() { countryname = "Select a country", value = "0" };
country _country1 = new country() { countryname = "India", value = "India" };
country _country2 = new country() { countryname = "USA", value = "USA" };
countries.Add(_country);
countries.Add(_country1);
countries.Add(_country2);
return countries;
}
Currently , i am using this function via
ViewData["country"] = GetCountryLists();
is it ok for me to use this same function like this one in the view, so that I do not need to use the viewdata object,
<%: Html.DropDownList("country", new SelectList(UserController.GetCountryLists(), "value", "countryname", "0"))%>
Kindly suggest me the best practice.
II. Also i have another query, when i use the same id & name for the radiobuttons, validation at the client side is working fine.
If I use the same condition for a group of checkboxes, i get do not get the checkboxes highlighted during client validation and only during server validation, i get the error message, but the controls [checkboxes] do not have a red border indicating the error.
I have used my own html helper to generate the checkboxlist as per http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs
Kindly let me know if there is any possible solution for this problem too. As i am new to asp.net mvc2, i am not sure of using these.. kindly suggest me accordingly.
is it ok for me to use this same function like this one in the view, so that I do not need to use the viewdata object
No, this is not good practice and violation of the MVC pattern for various reasons. Views shouldn't pull information. They should only use the information that it is being passed in the view model. It is the controller's responsibility to invoke various methods to fetch data and then construct a view model containing all the necessary information needed for the view and then pass this view model to the view. So here's the suggested way:
<%= Html.DropDownListFor(
x => x.Country,
new SelectList(Model.Countries, "value", "countryname")
) %>
or with the ugly/weakly typed/avoid to use/requiring magic strings ViewData:
<%= Html.DropDownList(
"Country",
new SelectList((IEnumerable)ViewData["Countries"], "value", "countryname")
) %>
and it is the responsibility of the control to populate either the view model properties or the ugly ViewData.
As far as your second question is concerned you will need to show your code in order to see what is wrong with it. From your description what I can say is that you cannot have two elements with the same id in the DOM or you get invalid markup.

Resources