how to Optimize multiple searches in the table - asp.net-core

I want to select a few fields from the table and search For example, a user records the project name, color project status, project date, and so on
For example, in the search form, I want to select the project name and color of the project status and project date, and fetch the information
But the problem is that I have to apply too many (if)
How can I select and search multiple fields with the least if ?
Please see the image and the codes and help.
if (hesabrsee.Projectid != 0 && red != 0 && yealoo != 0)
{
fraindList = fraindList.Where(p => p.Projectid == hesabrsee.Projectid && p.ControlState == red && p.ControlState == yealoo);
ViewBag.yealoo = "checked";
ViewBag.red = "checked";
return View(fraindList.ToList());
}
<div class="col-md-4 col-xs-12">
<label>نام پروژه</label>
<select class="form-control" id="form_frame" name="Projectid">
<option value="" default="" selected="">انتخاب کنید</option>
#foreach (var item in ViewBag.proList)
{
<option value="#item.projectID">#item.projectName</option>
}
</select>
</div>

You can try to use the following code:
string s = "select * from Hesabrsee where Projectid=" + hesabrsee.Projectid;
s += red != 0 ? "ControlState==red" : "";
s += green != 0 ? "ControlState==green" : "";
var fraindList = _context.Student.FromSqlRaw(s).AsEnumerable();
if (FramDate != null)
{
fraindList = fraindList.Where(l => l.ControlDate.Date >= ConvertDateTime.ConvertShamsiToMiladi(FramDate));
}
if (ToDate != null)
{
fraindList = fraindList.Where(l => l.ControlDate.Date >= ConvertDateTime.ConvertShamsiToMiladi(ToDate));
}
return View(fraindList.ToList());

Related

Update Product Table with subtable. Razor code and update code of controller is working fine but [HTTPPOST] code is not working, giving errors

#foreach (var sprd in ViewBag.subProduct)
{
<div class="form-row control-group row-fluid">
<div class="controls span2">
<select id="text" style="visibility: hidden;"></select>
</div>
<div class="controls span2">
#sprd.subproduct_pricetitle
#Html.Hidden("title1", (string)#sprd.subproduct_pricetitle)
</div>
#Html.Hidden("subid", (int)#sprd.subproduct_id)
<div class="controls span2">
<div class="input-prepend input-append row-fluid">
<span class="add-on prepend ">£ </span>
#Html.TextBox("combined-input1", (decimal)#sprd.subproduct_price, new { #class = "row-fluid" })
</div>
</div>
</div>
}
This is a code for displaying the data on view page.
public ActionResult Update(int id)
{
IEnumerable<producttype_master> Product_Typee;
int producttypeid = Convert.ToInt32(context.product_master.Find(id).product_type.ToString());
Product_Typee = context.producttype_master.ToList();
ViewBag.productType = new SelectList(Product_Typee, "producttype_id", "producttype_name", producttypeid);
IEnumerable<product_master> products;
products = context.product_master.ToList();
//List<subproduct_master> sprdList = context.subproduct_master.Where(m => m.product_id == id).ToList();
IEnumerable<subproduct_master> sprdList = context.subproduct_master.Where(m => m.product_id == id).ToList();
ViewBag.subProduct = sprdList;
return View(context.product_master.Find(id));
}
This update code is not working well, giving error.
[HttpPost]
public ActionResult Update(product_master prdmaster, subproduct_master subprdmaster, FormCollection formCollection)
{
product_master prd = context.product_master.Where(m => m.product_id == prdmaster.product_id).FirstOrDefault();
if (prd != null)
{
prd.product_type = int.Parse(formCollection["PrdType"]);
prd.product_updateby = 1;
prd.product_updateon = DateTime.Now;
prd.product_isactive = "1";
subproduct_master sprdList = context.subproduct_master.Where(m => m.product_id == prd.product_id).FirstOrDefault();
if (sprdList != null)
{
//string latestId = context.product_master.Max(p => p.product_id).ToString();
for (int i = 1; i <= 7; i++)
{
subproduct_master sprdList2 = context.subproduct_master.Where(m => m.subproduct_id == sprdList.subproduct_id).FirstOrDefault();
sprdList2.subproduct_price = decimal.Parse(formCollection["combined-input" + i]);
sprdList2.subproduct_pricevalidity = Convert.ToDateTime(formCollection["datepicker1"]);
sprdList2.subproduct_updatedon = DateTime.Now;
sprdList2.subproduct_updatedby = 1;
sprdList2.subproduct_isactive = "1";
context.Entry(sprdList2).CurrentValues.SetValues(sprdList);
//context.SaveChanges();
sprdList2.subproduct_id++;
}
}
context.Entry(prd).CurrentValues.SetValues(prdmaster);
context.SaveChanges();
}
return View(prdmaster);
}

How display the last n of records in MVC

i am taking last 4 four comments and want to display in the tooltip,
I am doing the below code but its showing the contents like "System.Collection.Generic.List"
var list = db.PO_Prd_Comments.Where(t => t.PO_TrgCal_ID == item.ID && t.Reply == false).OrderByDescending(t => t.PO_TrgCal_ID).Take(4).ToList();
List<string> comments = new List<string>();
if (list.Count != 0)
{
foreach (var ts in list)
{
comments.Add(ts.PrdComment);
if (list.Count == 1)
{
notify = list.SingleOrDefault().notify;
}
else
{
notify = true;
}
}
}
<td>
<a href="#" onclick="popup4(#CountID)" title="#comments"
<img src="~/Images/comment.GIF"/></a>
</td>
How i display these four comments in the tooltip.
comments is a list of string. You need to convert that to a single string and use that as the title property value.
<a href="#" onclick="popup4(#CountID)" title="#String.Join(Environment.NewLine,comments)">
<img src="~/Images/comment.GIF"/>
</a>

The local variable assignment was printed as output on asp.net mvc

I have a script like below on my View:
#{int count = 0;}
#foreach (var item in Model)
{
<div >
<div class="col-md-4"><img src="#item.ImageLink" />
#(count = count +1 );
#if(count ==3)
{
#(count = 0);
#Html.Raw("<div class=\"clearfix visible-md\"></div>");
}
</div>
</div>
}
The problem here is : in addition to the imageLink, the output also contain the value of count when it is assigned with value (meaning that it will print some number: 1,2,3,0 - which is value of count in each loop)
How can I resolve it?
Thanks & regards,
If you use parentheses it's output will be submitted as part of the HTML. You can use curly braces for code block as below.
#{
int count = 0;
foreach (var item in Model)
{
<div>
<div class="col-md-4">
<img src="#item.ImageLink" />
#{count = count + 1;}
#if (count == 3)
{
count = 0;
#Html.Raw("<div class=\"clearfix visible-md\"></div>");
}
</div>
</div>
}
}

How do render shape of a specific ContentPart of a ContentItem from alternative razor file?

Considering we have a simple ContentType named Product which has some fields and ContentParts such CommentPart. When we create an alternative for Product (mytheme/Views/Content-Product.Detail.cshtml) then every fields and parts should be render from the alternative file. for example we can access to fields value and display theme.
But my question is that how to render default shape of the parts of the ContentType from our alternative file?
for example the Product has CommentPart and i want to call the default Display of it in a arbitrary place of view:
#using Orchard.Utility.Extensions;
#{
if (Model.Title != null)
{
Layout.Title = Model.Title;
}
Model.Classes.Add("content-item");
var contentTypeClassName = ((string)Model.ContentItem.ContentType).HtmlClassify();
Model.Classes.Add(contentTypeClassName);
var tag = Tag(Model, "article");
var Title = Model.ContentItem.TitlePart.Title;
var CreatedUtc = Model.ContentItem.CommonPart.CreatedUtc;
var Path = Model.ContentItem.AutoroutePart.Path;
var Thumb = Model.ContentItem.Product.ThumbnailImage.FileName;
var Price = Model.ContentItem.Product.Price.Value;
var CooperatorPrice = Model.ContentItem.Product.CooperatorPrice.Value;
var Discount = Model.ContentItem.Product.Discount.Value;
var Available = Model.ContentItem.Product.Available.Value ?? false;
var Special = Model.ContentItem.Product.Special.Value ?? false;
}
#tag.StartElement
<a href="#Path" >
#if (Special != null)
{
<img class="special" src="#Url.Content("Themes/RayanTechTheme/Content/Images/specialoffer.png")" />
}
#if (Thumb != null)
{
<img class="thumb" src="#Url.Content(Thumb)" />
}
<header>#Title</header>
#if (Available != null)
{
<span class="available"></span>
}
#if (Price != null && Price > 0)
{
<span class="price">#RayanTechTheme.Helpers.CurrencyHelper.GetSeperatedCurrency(Price) تومان</span>
}
#if (Price != null && Price > 0 && Discount != null && Discount > 0)
{
<span class="prev-price">#RayanTechTheme.Helpers.CurrencyHelper.GetSeperatedCurrency(Price + Discount) تومان</span>
}
#if (CooperatorPrice != null && CooperatorPrice > 0)
{
<span class="cooperatorprice">#RayanTechTheme.Helpers.CurrencyHelper.GetSeperatedCurrency(CooperatorPrice) تومان</span>
}
#if (Discount != null && Discount > 0)
{
<span class="discount">#RayanTechTheme.Helpers.CurrencyHelper.GetSeperatedCurrency(Discount) تومان</span>
}
#if (CreatedUtc != null)
{
<span class="createdutc">#RayanTechTheme.Helpers.DateHelper.GetPersianDate(CreatedUtc)</span>
}
<div class="more">
#Display.Parts_CommentForm(Model.ContentItem.CommentPart) /// >>>>> HOW DO IT???
</div>
</a>
#tag.EndElement
How do i implement the last <div> as i mentioned?
The best way to do this is by using Orchard's Placement.
Rewrite your <div> to look like:
<div class="more">
#Display(Model.Comments)
</div>
Then in your Placement.info
<Match ContentType="MyContentType">
<Match DisplayType="Detail">
<Place Parts_CommentForm="Comments:1" />
</Match>
</Match>
When your ContentItem Shape is being rendered, the Shapes for each of its parts are already built ready for display (I think, someone like Bertrand Le Roy could correct me on this if I'm wrong).
The #Display() helper is for rendering Shapes, which are created from a Part's Driver. The Placement.info tells Orchard where these Shapes should display.
See also: https://stackoverflow.com/a/19388208/1053199

Populating a drop down list with non-repeating values

I have a multiple search query which includes a drop down to pick an admin which needs to be called from the Assigned_To column in my RMS db. The problem is that the dropdown shows every single value repeated. For example:
Admin:
Not assigned
Not assigned
"
"
"
Alexandra M.
Martin D.
Alexandra M.
I want it to look like:
Admin:
Not assigned
Martin D.
Alexandra M.
"Not assigned" is the default value of a case that is created.
Here is my code:
var db = Database.Open("RMS") ;
var selectCommand = "SELECT * FROM RMS";
var formSSO = "";
var formAssigned_To = "";
var formCase_Status= "";
var fromDate = "";
var toDate = "";
formSSO = Request.QueryString["formSSO"];
formAssigned_To = "%" + Request["formAssigned_To"] + "%";
formCase_Status = Request["formCase_Status"];
fromDate = Request.QueryString["fromDate"];
toDate = Request.QueryString["toDate"];
selectCommand = "SELECT DISTINCT * FROM RMS WHERE 1=1";
if(!Request.QueryString["formSSO"].IsEmpty() ) {
selectCommand +=" AND SSO LIKE #0";
}
if(!Request.QueryString["formAssigned_To"].IsEmpty() ) {
selectCommand +=" AND Assigned_To LIKE #1";
}
if(!Request.QueryString["formCase_Status"].IsEmpty() ) {
selectCommand +=" AND Case_Status = #2";
}
if(!Request.QueryString["fromDate"].IsEmpty() ) {
selectCommand +=" AND Created >= #3";
}
if(!Request.QueryString["toDate"].IsEmpty() ) {
selectCommand +=" AND Created <= #4";
}
var data = db.Query(selectCommand, formSSO, formAssigned_To, formCase_Status, fromDate, toDate);
var columns = new[]{"ID", "SSO", "Category", "System", "Subject", "Created", "Assigned_To"};
var grid = new WebGrid(data, ajaxUpdateContainerId: "grid", defaultSort: "ID", columnNames: columns);
if (Request.QueryString[grid.SortDirectionFieldName].IsEmpty()) {
grid.SortDirection = SortDirection.Descending;
}
}
And this is the html form field for the dropdown:
<li class="form-line form-line-column" id="id_22">
<label class="form-label-top" id="label_22" for="input_22"> ADMIN </label>
<div id="cid_22" class="form-input-wide">
<select name="formAssigned_To">
#foreach(var row in data){
<option></option>
<option value="#row.ID">#row.Assigned_To</option>
}
</select>
</div>
</li>
Can't you just use
SELECT DISTINCT Assigned_To FROM RMS WHERE 1=1
That would give you the distinct values for the dropdown, otherwise you would have to do it
from the returned results.
I figured it out! I changed my form field to this: (Which has its own SELECT DISTINCT because it was specific to this field.
<li class="form-line form-line-column" id="id_22">
<label class="form-label-top" id="label_22" for="input_22"> ADMIN </label>
<div id="cid_22" class="form-input-wide">
<select name="formAssigned_To">
<option></option>
#foreach(var row in db.Query("SELECT DISTINCT Assigned_To FROM RMS")){
<option value="#row.Assigned_To">#row.Assigned_To</option>
}
</select>
</div>
</li>
I left my selectCommand like this:
selectCommand = "SELECT * FROM RMS WHERE 1=1";
This was thanks to: CSHTML - SQL QUERY....WHERE NAME=#NameOne