Query ViewData, then change value based on result - asp.net-mvc

I have this code snippet in my controller
outputmodel4.Add(new SP_data.student()
{
student_id = (decimal)SPOutput4["student_id"],
student_no = (string)SPOutput4["student_no"],
course_id = (decimal)SPOutput4["course_id"],
floor_no = (int)SPOutput4["floor_no"],
tutor_id = (decimal)SPOutput4["tutor_id"],
capacity_id = (decimal)SPOutput4["capacity_id"],
});
}
ViewData["Output"] = outputmodel4;
what I am trying to do it query Capacity_id, and if the number is (say) 2, then display "All Most Full" on my view.
I think the say to accomplish this is
#foreach (var item in ViewData["Output"] as IEnumerable<app.Models.SP_data.student>)
{
<TD>
#if (item.capacity_id = 2)
{
All Most Full
}
else if (item.capacity_id = 5)
{
Full
}
else if etc..
</TD>
}
.. but I getting errors in the view
Cannot implicitly convert type decimal to bool
can someone please advise, and help ?
thanks

Related

how to Show deleted comments in admin

I want to put the list of deleted comments in the admin section. IsDelete capability also has a comments section. The code works fine, but when I put the where condition, the output value is zero and no comments are displayed.
public ShowCommentUsersForAdminViewModel GetRemovedCommentByAdminSearch(int pageId = 1, string filter = "")
{
//lazy load enable with IQueryable
IQueryable<BlogComment> result = _context.BlogComments
.Include(c => c.Blog)
.Include(c => c.User);
if (!string.IsNullOrEmpty(filter))
{
result = result.Where(u => u.Comment.Contains(filter) || u.User.UserName.Contains(filter) || u.Blog.BlogTitle.Contains(filter));
}
//show Item In Page
int take = 10;
int skip = (pageId - 1) * take;
ShowCommentUsersForAdminViewModel list = new ShowCommentUsersForAdminViewModel();
list.CurrentPage = pageId;
list.PageCount = (result.Count() / take) + 1;
list.BlogComments = result.Where(c => c.IsDelete).OrderBy(u => u.CreateDateComment).Skip(skip).Take(take).ToList();
return list;
}
yes I used this code
modelBuilder.Entity<ProductComment>().HasQueryFilter(b => !b.IsDelete);
Reviewed the code again, and used IgnoreQueryFilters() problem solved.
thanks Ivan Stoev

TFS 2012 API Get Field SUMMARY Object TESTCASE

SUMMARY get the field as in the TestCase object, I get the code to retrieve the Steps and Expected Results, No SUMMARY.
Please your help.
thank you
for (int i = 0; i < testPlan.TestSuites.Count; i++)
{
var ts = testPlan.TestSuites[i];
ITestSuiteEntryCollection suiteentrys = ts.TestCases;
foreach (ITestSuiteEntry item in suiteentrys)
{
ITestCase tes = item.TestCase;
tfsTestCase testcase = new tfsTestCase();
testcase.Suite = testPlan.TestSuites[i].Title;
testcase.Nombre = tes.Title;
foreach (ITestAction action in tes.Actions)
{
ITestStep er = action as ITestStep;
testcase.NumeroPaso = er.Id;
testcase.Paso = er.Title;
testcase.ResultadoEsperado = er.ExpectedResult;
testcase.Descripcion = er.Summary ??? ;
}
lsttestcase.Add(testcase);
}
}
}
the test-step doesn't have a summary. The test-case is comprised of test-steps, and it's the test-case that has the summary.
You can try with
testCase.CustomFields["Summary"];

Is there an easier way than this to edit a table in my database?

I’m trying to update a row in a table. I found a way to make it work but I’m wondering if there is an easier or better way. Please help me with any ideas that you may have. Thanks
Here’s the code…
var courseRubics = from r in db.Rubrics where r.DepartmentID == 2 select r;
var selectedRubrics = courseRubics.Select(r => r.RubricID);
List<int> rubricsList = selectedRubrics.ToList();
foreach (var rub in courseRubics.ToList())
{
if (!String.IsNullOrEmpty(formCollection["item.Weight"]))
{
Rubric aRubic1 = db.Rubrics.Find(1);
Rubric updateRubic1 = (Rubric)aRubic1;
int rubric1 = Convert.ToInt32(totlrubric[0]);
updateRubic1.Weight = rubric1;
Rubric aRubic2 = db.Rubrics.Find(2);
Rubric updateRubic2 = (Rubric)aRubic2;
int rubric2 = Convert.ToInt32(totlrubric[1]);
updateRubic2.Weight = rubric2;
Rubric aRubic3 = db.Rubrics.Find(4);
Rubric updateRubic3 = (Rubric)aRubic3;
int rubric3 = Convert.ToInt32(totlrubric[2]);
updateRubic3.Weight = rubric3;
}
db.SaveChanges();
}
Couple of ideas / observations
Your initial set of selects could be narrowed down to (sorry for the alternate syntax)
var list = db.Rubrics.Where(r => r.DepartmentId == 2).Select(x => x.RubricId).ToList()
You iterate over foreach (var rub in courseRubics.ToList()) but don't seem to be doing anything with rub
The contents of the if... could be tightened up a little. I don't see any reason why you need to do the casting or why you can't just inline the creation of rubricX,
if (!String.IsNullOrEmpty(formCollection["item.Weight"]))
{
Rubric aRubic1 = db.Rubrics.Find(1);
aRubic1.Weight = Convert.ToInt32(totlrubric[0]);
Rubric aRubic2 = db.Rubrics.Find(2);
aRubic2.Weight = Convert.ToInt32(totlrubric[1]);
Rubric aRubic3 = db.Rubrics.Find(4);
aRubic3.Weight = Convert.ToInt32(totlrubric[2]);
}

Make good LINQ query with string[]

I have some problems with LINQ and maby someone got answers
string[] roleNames = Roles.GetRolesForUser(currentUserName);
result = context.MenuRoles.Select(mr => new MenuGenerateViewModel
{
MenuID = mr.MenuID,
MenuNazwa = mr.Menu.MenuNazwa,
MenuKolejnosc = mr.Menu.MenuKolejnosc,
MenuStyl = mr.Menu.MenuStyl,
MenuParentID = mr.Menu.MenuParentID,
MenuActive = mr.Menu.MenuActive,
MenuActionName = mr.Menu.MenuAction.MenuActionName,
MenuControlName = mr.Menu.MenuControl.MenuControlName,
RoleName = mr.Role.RoleName,
RoleID = mr.RoleID,
MenuID = mr.MenuID
})
.Where(mr => mr.MenuActive == true)
.ToList();
How to take only compare string[] roleNames and return only if match. Problem alwais is when user is in the 2 or more roles.
Tx for answers
If I understand what you are asking for, add a second condition to your Where clause:
.Where(mr => mr.MenuActive && roleNames.Contains(mr.Role.RoleName))
You would be better off switching round your Where clause and Select for the simple reason that then you will not be retrieving from the database records which are not required.
result = context.MenuRoles.Where(mr => mr.MenuActive
&& roleNames.Contains(mr.Role.RoleName))
.Select(mr => ... )
.ToList();
This will generate a sql which only selects the necessary records, instead of selecting the whole lot and then filtering it. Try it and watch SQL profiler to see the difference (useful skill in any case when using EF)
With the help of brilliant people here, reached the target.
string[] roleNames = Roles.GetRolesForUser(currentUserName);
result = context.MenuRoles
.Where(mr => mr.Menu.MenuActive && roleNames.Contains(mr.Role.RoleName))
.Select(mr => new MenuGenerateViewModel
{
MenuID = mr.MenuID,
MenuNazwa = mr.Menu.MenuNazwa,
MenuKolejnosc = mr.Menu.MenuKolejnosc,
MenuStyl = mr.Menu.MenuStyl,
MenuParentID = mr.Menu.MenuParentID,
MenuActive = mr.Menu.MenuActive,
MenuActionName = mr.Menu.MenuAction.MenuActionName,
MenuControlName = mr.Menu.MenuControl.MenuControlName,
RoleName = mr.Role.RoleName
})
.ToList();
var userresult = context.MenuUsers
.Where(mr => mr.Menu.MenuActive && mr.User.Username == currentUserName)
.Select(mr => new MenuGenerateViewModel
{
MenuID = mr.MenuID,
MenuNazwa = mr.Menu.MenuNazwa,
MenuKolejnosc = mr.Menu.MenuKolejnosc,
MenuStyl = mr.Menu.MenuStyl,
MenuParentID = mr.Menu.MenuParentID,
MenuActive = mr.Menu.MenuActive,
MenuActionName = mr.Menu.MenuAction.MenuActionName,
MenuControlName = mr.Menu.MenuControl.MenuControlName,
Username = mr.User.Username
})
.ToList();
Here, gets all the menu to which you have the right, both through group membership and assigned directly to the menu itself.
// Kick all duplicates
var noduplicates = result.Concat(userresult)
.Distinct(new RoleMenuGenerateComparer());
Because usually we do not want duplicates in the menu so we remove them. For this to work properly we need to implement IEqualityComparer (U can read about this little bit up)
public class RoleMenuGenerateComparer : IEqualityComparer<MenuGenerateViewModel>
{
public bool Equals(MenuGenerateViewModel x, MenuGenerateViewModel y)
{
//Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y)) return true;
//Check whether any of the compared objects is null.
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
//Check whether the products' properties are equal.
return x.MenuNazwa == y.MenuNazwa && x.MenuID == y.MenuID;
}
public int GetHashCode(MenuGenerateViewModel menuGenerateViewModel)
{
if (Object.ReferenceEquals(menuGenerateViewModel, null)) return 0;
int hashMenuName = menuGenerateViewModel.MenuNazwa == null ? 0 : menuGenerateViewModel.MenuNazwa.GetHashCode();
int hashMenuID = menuGenerateViewModel.MenuID == null ? 0 : menuGenerateViewModel.MenuID.GetHashCode();
return hashMenuName ^ hashMenuID;
}
}
Of course I believe that you can optimize this code, but for the moment I have something like this.
Tx all for help.

ActionLink only if a certain condition is met

If s = "N/A" then I don't want to use the ActionLink. In other words, if the inventory item is not currently being used on a project, then don't provide the link (just show N/A instead). Also, how do I send the link to Projects/Details? Right now, it will go to "Nails/Projects/Details" instead, because I'm using the NailsController class.
<td class="table-normal-data">
<% Dim l As Integer = InStr(item.CurrentProject, " [")
Dim s As String = item.CurrentProject
Dim projectID As String = ""
If l > 0 Then
s = Mid(item.CurrentProject, 1, l - 1)
projectID = Mid(item.CurrentProject, l + 2, Len(item.CurrentProject) - l - 2)
Else
s = ""
End If
%>
<%: Html.ActionLink(s, "Projects/Details", New With {.id = projectID}) %>
</td>
I'm much more familiar with MVC3/Razor and C#, but I often do something like this in my views:
#if( Model.Flag )
{
<span>n/a</span>
}
else
{
#Html.ActionLink(....
}
Basically, you output different stuff through the view depending on the state of the model.

Resources