Razor default for dropdown box - vb.net

I have a drop down list written in Razor for a MVC app I am working on as:
#Html.DropDownList("BillId", "")
However the user does not have to select anything according to the logic of my program (the list is populated with 'Bill' objects in my controller). If they do not select any thing I get an error
The ViewData item that has the key 'BillId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.
How do I write a statement in Razor to return a BillId of 0 if nothing is selected?
I am not sure of the syntax as I have a background in straight java and VB but something alongs the line of
If DropdownBox.SelectedIndex = 0
Else
BillId = DropdownBox.SelectedIndex
End
Controller as follows:
Function Create(id As Integer) As ViewResult
ViewBag.id = id
Dim job As Job = New Job
job.CustomerId = id
job.JobAmount = 0
job.JobDate = Date.Now()
job.JobStatus = "Active"
Dim BillList = New List(Of Bill)()
Dim BillQuery = From s In db.Bills
Select s
BillList.AddRange(BillQuery)
ViewBag.BillIdList = New SelectList(BillList, "BillId", "BillDate")
ViewBag.BillId = New SelectList(BillList, "BillId", "BillDate")
Return View(job)
End Function
The POST function for create is as below:
<HttpPost()>
Function Create(job As Job) As ActionResult
If ModelState.IsValid Then
db.Jobs.Add(job)
db.SaveChanges()
Dim customer As Customer = db.Customers.Find(job.CustomerId)
Dim customerNumber As String = customer.CustCellphone.ToString()
Dim messageSender As SendMessage = New SendMessage
Dim smsMessage As String = "LAUNDRY: Job Number " & job.JobId & " has been booked in. You will be notified when individual services within it are ready for collection."
messageSender.SendMessage(smsMessage, customerNumber)
Dim url As String = "/RequestedService/AddService/" + job.JobId.ToString()
Return Redirect(url)
End If
Return View(job)
End Function
EDIT
I was wondering too how this is passed back as in the POST I may be able to check for a 'null'? However I feel the problem may be the moment the submit button is pressed

In your POST controller action you forgot to populate the ViewCrap (oops, I meant ViewBag) before returning the view:
<HttpPost()>
Function Create(job As Job) As ActionResult
If ModelState.IsValid Then
...
End If
' Here you must populate the ViewCrap before returning the view the same
' way you did in your GET action because your view depend on it
Dim BillQuery = From s In db.Bills
Select s
ViewBag.BillId = New SelectList(BillQuery.ToList(), "BillId", "BillDate")
Return View(job)
End Function
But I would hyper strongly recommend you to use view models and forget about the existence of the ...... (the word that I don't want to pronounce).
UPDATE:
Now let's look at the correct way to implement this (which is by using view models). A view model is a class that you should define for each of your views and which will represent its specific requirements. So from what you have said in the comments section to my answer you want to have a dropdown list in your view where the user has to select a bill from a dropdown and which is required.
So let's roll the view model:
public class JobViewModel
{
[Required(ErrorMessage = "Please select a bill")]
[Display(Name = "Bill")]
public int? SelectedBillId { get; set; }
public IEnumerable<SelectListItem> Bills
{
get
{
return db.Bills.ToList().Select(x => new SelectListItem
{
Value = x.BillId.ToString(),
Text = x.BillDate.ToString()
});
}
}
public int CustomerId { get; set; }
... here you could put any other properties that you want
to display on the view, things like JobId, ...
}
then we define our controller with the 2 actions:
public ActionResult Create(int id)
{
var model = new JobViewModel
{
CustomerId = id
};
return View(model);
}
[HttpPost]
public ActionResult Create(JobViewModel model)
{
if (ModelState.IsValid)
{
// Using AutoMapper here to map between the domain model
// and the view model (http://automapper.org/)
var job = Mapper.Map<JobViewModel, Job>(model);
// Now call your service layer to do the necessary processings
// on this job domain model including saving the job and sending
// messages and stuff. This avoids polluting your controller with
// business logic code which belongs to your service layer
ServiceLayer.ProcessJob(job);
return RedirectToAction("AddService", "RequestedService", new { id = job.JobId });
}
return View(model);
}
and finally you will have a corresponding view which will be strongly typed to the view model:
#model JobViewModel
#using (Html.BeginForm())
{
<div>
#Html.LabelFor(x => x.SelectedBillId)
#Html.DropDownListFor(x => x.SelectedBillId, Model.Bills, "-- select --")
#Html.ValidationMessageFor(x => x.SelectedBillId)
</div>
... some other input fields
<p><button type="submit">OK</button></p>
}
And now, as promised in the comments section let me show what I dubbed the absolute pornographic approach to solve this and which if you implemented in your application I will have to ask you to no longer come back and ask any ASP.NET MVC related question on StackOverflow :-)
The pornographic approach consisted into manually inserting an item with id = 0 and text = empty string into the beginning of the list and then inside the controller verifying if the selected id equals 0 in order to check whether the model is valid or not:
So in your GET action:
Function Create(id As Integer) As ViewResult
ViewBag.id = id
Dim job As Job = New Job
job.CustomerId = id
job.JobAmount = 0
job.JobDate = Date.Now()
job.JobStatus = "Active"
Dim Bills = db.Bills.ToList().Select(Function(s) New SelectListItem With { .Value = s.BillId.ToString(), .Text = s.BillDate.ToString() })
Bills.Insert(0, New SelectListItem With { .Value = "0", .Text = "" })
ViewBag.BillId = Bills
Return View(job)
End Function
<HttpPost()>
Function Create(job As Job, BillId as Integer) As ActionResult
If BillId > 0 Then
db.Jobs.Add(job)
db.SaveChanges()
Dim customer As Customer = db.Customers.Find(job.CustomerId)
Dim customerNumber As String = customer.CustCellphone.ToString()
Dim messageSender As SendMessage = New SendMessage
Dim smsMessage As String = "LAUNDRY: Job Number " & job.JobId & " has been booked in. You will be notified when individual services within it are ready for collection."
messageSender.SendMessage(smsMessage, customerNumber)
Dim url As String = "/RequestedService/AddService/" + job.JobId.ToString()
Return Redirect(url)
End If
ModelState.AddModelError("BillId", "Please select a bill")
Return View(job)
End Function
and inside the view:
#Html.DropDownList("BillId")

Related

Adding quantity if the product is exist in the DataGridView

Can someone help me with my problem?
The picture below shows the same item I inputted. What I want is I don't like to show duplicate items in DataGridView. If the same product record adds, then the new will not be showed, it just add the quantity when clicking the "Save" button. And I don't know how to code it I'm just new to vb.net. Can somebody help me how to do it?? It would be a big help for me if you do, thank you so much!
Below is my code for Save button:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
initializeCon()
Dim found As Boolean = False
If (DataGridView1.Rows.Count > 0) Then
For Each row As DataGridViewRow In DataGridView1.Rows
If (Convert.ToString(row.Cells(1).Value) = dtDate.Text) And (Convert.ToString(row.Cells(2).Value) = txtProductCode.Text) AndAlso
(Convert.ToString(row.Cells(3).Value) = txtProductName.Text) Then
row.Cells(4).Value = Convert.ToString(txtQuantity.Text + Convert.ToInt16(row.Cells(4).Value))
found = True
End If
Next
If Not found Then
cmd = New SqlCommand("INSERT INTO tbl_productOrders VALUES('" & txtID.Text & "','" & dtDate.Text & "','" & txtProductCode.Text & "','" & txtProductName.Text & "'," & txtQuantity.Text & ");", con)
cmd.ExecuteNonQuery()
clrtxt()
SaveMsg()
Getdata()
End If
End If
End Sub
this is an axample to avoid duplicates while adding rows
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim exist As Boolean = False, numrow As Integer = 0, numtext As Integer
For Each itm As DataGridViewRow In DataGridView1.Rows
If itm.Cells(0).Value IsNot Nothing Then
If itm.Cells(0).Value.ToString = TextBox1.Text Then
exist = True
numrow = itm.Index
numtext = CInt(itm.Cells(1).Value)
Exit For
End If
End If
Next
If exist = False Then
DataGridView1.Rows.Add(New String() {TextBox1.Text, TextBox2.Text})
Else
DataGridView1.Rows(numrow).Cells(1).Value = CInt(TextBox2.Text) + numtext
End If
End Sub
There are two aspects to improve your program.
1. Make comparing products better
Since you need to check individual properties to find equal product, the better way is to overload == operator along with implementing IEquatable interface. You can read more here. When you do this, you can compare products with == operator: if (product1 == product2) { }. In this case three properties are being compared. If they all are the same, then two products are equal.
2. Make adding products to DataGridView easier
In order to make adding products to DataGridView easier, you need to leverage the handy binding mechanism in Windows Forms, which appeared in .NET Framework 2.0 - BindingSource. It acts like a medium between your data and controls. In order to make this class usable, you need to use another handy class - BindingList. You feed BindingList to BindingSource and assign BindingSource to DataSource property of DataGridView. After that you work only with BindingList collection (adding/removing/editing) - and all the propagation is done by BindinSource.
To summarize, here is the explanation and full code. Here you can download the project itself.
When you add product, the code first checks whether such product already exists in DataGridView (Add product button). Do note that we don't work with DataGridView directly - we work only with the collection. Thanks to enhancements, we can use LINQ here. If the product is not found, we add it to collection. If it's found, we just update the quantity.
If you need to update selected product data (Change quantity button), you only need to retrieve it from selected rows and use BoundDataItem - this is where our product is located. Then just update properties of product you need.
If you need to remove product (Delete product button), retrieve it from selected row and delete it from the collection.
Important! Do not forget to call Refresh() method on DataGridView after all taken actions in order to see the changes.
namespace WinFormsApp
{
public partial class Root : Form
{
private BindingList<Product> list = null;
private BindingSource bindingSource = null;
public Root() => InitializeComponent();
private void OnFormLoaded(object sender, EventArgs e)
{
// Our collection holding products
list = new BindingList<Product>();
// I've set up columns manually (with applied binding),
// so switch generating columns off.
dataGrid.AutoGenerateColumns = false;
// Disable adding new row
dataGrid.AllowUserToAddRows = false;
// Create our medium between grid and collection
bindingSource = new BindingSource { DataSource = list };
// Set binding
dataGrid.DataSource = bindingSource;
}
private void OnAddRecord(object sender, EventArgs e)
{
// Create new product
var new_product = new Product
{
Date = dtPicker.Value.ToShortDateString(),
Code = txtCode.Text,
Name = txtName.Text,
Quantity = npQuantity.Value
};
// No need to check individual properties here
// as == and IEquatable will do all the work.
// We can safely use LINQ here.
var p = list.FirstOrDefault(x => x == new_product);
if (p == null)
{
// Product is not found. Add it to list.
list.Add(new_product);
}
else
{
// Product is found. Update quantity.
p.Quantity += new_product.Quantity;
}
dataGrid.Refresh(); //Required to reflect changes
}
private void OnChangeQuantity(object sender, EventArgs e)
{
// Change quantity here.
var product = GetProduct();
if (product != null)
{
// Update product's quantity.
product.Quantity *= 2;
// Do not forget to refresh grid.
dataGrid.Refresh();
}
}
private void OnDeleteProduct(object sender, EventArgs e)
{
// Delete product here.
var product = GetProduct();
if (product != null)
{
// We need to delete product only from collection
list.Remove(product);
// Do not forget to refresh grid
dataGrid.Refresh();
}
}
// Retrieve product from selected row.
private Product GetProduct() =>
dataGrid.SelectedCells.Count == 0 ?
null :
dataGrid.SelectedCells[0].OwningRow.DataBoundItem as Product;
}
class Product : IEquatable<Product>
{
public string Date { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public decimal Quantity { get; set; }
// Overload == operator
public static bool operator ==(Product firstProduct, Product secondProduct)
{
// Check for null on left side.
if (Object.ReferenceEquals(firstProduct, null))
{
if (Object.ReferenceEquals(secondProduct, null))
{
// null == null = true.
return true;
}
// Only the left side is null.
return false;
}
// Equals handles case of null on right side.
return firstProduct.Equals(secondProduct);
}
// Overload != operator (required when overloading == operator)
public static bool operator !=(Product firstProduct, Product secondProduct) =>
!(firstProduct == secondProduct);
// Implementing IEquatable<T> interface
public bool Equals(Product other)
{
// If 'other' is null, return false.
if (Object.ReferenceEquals(other, null))
{
return false;
}
// Optimization for a common success case.
if (Object.ReferenceEquals(this, other))
{
return true;
}
// If run-time types are not exactly the same, return false.
if (this.GetType() != other.GetType())
{
return false;
}
// Return true if the fields match.
return
Date == other.Date &&
Code == other.Code &&
Name == other.Name;
}
public override bool Equals(object obj) => this.Equals(obj as Product);
public override int GetHashCode() =>
Date.GetHashCode() + Code.GetHashCode() + Name.GetHashCode();
// Optional. For debugging purposes.
public override string ToString() =>
$"Date: {Date}, Code: {Code}, Name: {Name}, Quantity: {Quantity}";
}
}

I want to select different packages on the basis of states, how to get value

public ActionResult Rajasthan()
{
//List<PackageGallery> all = new List<PackageGallery>();
using (travelAndTourismEntities objentity = new travelAndTourismEntities())
{
List<PackageGallery> all = (from p in objentity.PackageGalleries where p.ParentCategory == "Rajasthan" orderby p.Imageid select p).ToList();
// all = objentity.PackageGalleries.ToList();
return View(all);
}
}
I am writing this query but this is specific to rajasthan only how to make it generalize
You can create a parameter to your action method where you accept the state name you want to use in your filter.
public ActionResult PackageGalleries(string id)
{
var all = new List<PackageGallery>();
using (var db = new travelAndTourismEntities())
{
all = db.PackageGalleries
.Where(s=>s.ParentCategory==id)
.OrderBy(x=>x.ImageId).ToList();
}
return View(all);
}
And you can call it like yourSiteName/yourControllerName/PackageGalleries/rajasthan or yourSiteName/yourControllerName/PackageGalleries/kerala
The last part of the url will be mapped to the id parameter of the action method.

I want to keep 2 values for next save from preivous save. MVC 4

User enters PWS (public water system), LabID. Then clicks Save button.
I would like these values to populate the new input form that right now gets emptied out on a succesful save.
#Html.TextBoxFor(model => model.PWS, new { #autofocus = "autofocus", #style="width:50px", #maxlength="5" })
Controller ActionResult
First time through:
[HttpGet]
public ActionResult AddColiform(string sortorder)
{
int batchid;
batchid = Convert.ToInt32(Session["ThisBatch"]);
//Session["ThisBatch"] = batchid;
ViewBag.Methods = FillMethods();
ViewBag.Latest = (from m in _db.BactiBucket
where m.Batch_ID == batchid
select m).ToList();
ViewBag.ThisBatch = batchid;
return View(new BactiBucket());
}
When Save button clicked:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddColiform(BactiBucket bucket)
{
if (ModelState.IsValid)
{
//FIRST RECORD SAVED FOR USER CREATES BATCH, OTHERWISE BATCH IS ZERO
if (Session["ThisBatch"].Equals(0))
{
var newbatchid = CheckAndMakeBatchIfNone();
Session["ThisBatch"] = newbatchid;
bucket.Batch_ID = newbatchid;
}
_db.AddToBactiBucket(bucket);
_db.SaveChanges();
return RedirectToAction("AddColiform");
}
ViewBag.Methods = FillMethods();
int batchid;
batchid = Convert.ToInt32(Session["ThisBatch"]);
ViewBag.ThisBatch = batchid;
ViewBag.Latest = (from m in _db.BactiBucket
where m.Batch_ID == batchid
select m).ToList();
return View(bucket);
}
You can pass additional parameters to your GET method in the redirect, and use ths values to set the properties of your model (note its not clear why your method has a parameter string sortorder when you never use it)
[HttpGet]
public ActionResult AddColiform(string sortorder, string PWS, string LabID)
{
....
BactiBucket model = new BactiBucket() { PWS = PWS, LabID = LabID };
return View(model);
}
[HttpPost]
public ActionResult AddColiform(BactiBucket bucket)
{
if (ModelState.IsValid)
{
....
return RedirectToAction("AddColiform", new { PWS = bucket.PWS, LabID = bucket.LabID });
}
....
}
Ok, if it was a snake it would have bit me.
In the declaration of the ActionResult I pass the values of the textboxes to the controller. It comes in with the Post action. (PWS and LabID are the names of the inputs).
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddColiform(BactiBucket bucket, string PWS, string LabID)
Then right before the return RedirectToAction("AddColiform");
I set Session variables for each value:
Session["PWS"]=PWS;
Session["LabID"]=LabID;
of course I might use ViewBag.PWS and ViewBag.LabID
Then, when returning and building the new Add Record form,
I populate the #Value of each textbox respectfully:
#Html.TextBoxFor(model => model.PWS, new {#Value=Session["PWS"], #autofocus = "autofocus", #style="width:50px", #maxlength="5" })
#Html.TextBoxFor(model => model.LabID, new {#Value=Session["LabID"], #style="width:150px", #maxlength="20" })
Since I haven't run this code I know I will have to check if Session objects aren't null. Or ViewBag objects. Or set them to "" first time through.
I got this from this forum thread

How to get the selected value of a dynamically created selectlist component

I am not sure whether this is even possible, but I am trying to get the values of dynamically created selectlists in a VisualForce Apex controller class.
I am dynamically creating a selectlist for each field in a particular object (e.g. Contact) using the code below, but now I do not know how to get the selected value back. I have tried setting the value of each in the constructor and on a separate line (not in code sample below), but this does not seem to work.
VisualForce page:
<apex:dynamicComponent componentValue="{!contactPageBlockSection}" />
Apex controller:
public Component.Apex.PageBlockSection GetContactPageBlockSection(string objectName)
{
Map<string, Schema.SObjectField> FieldMap;
FieldMap = Schema.SObjectType.Contact.fields.getMap();
Set<string> FieldSet = FieldMap.keySet();
List<string> FieldList = new List<string>();
FieldList.addAll(FieldSet);
FieldList.sort();
Component.Apex.PageBlockSection pbs = new Component.Apex.PageBlockSection(columns = 2);
for (string fieldName : FieldList)
{
Component.Apex.PageBlockSectionItem pbsi = new Component.Apex.PageBlockSectionItem();
Schema.DescribeFieldResult field = (FieldMap.get(fieldName)).getDescribe();
if (field.isUpdateable() && field.IsAccessible())
{
Schema.DisplayType dt = field.getType();
Component.Apex.OutputLabel lblText = new Component.Apex.OutputLabel(escape = false);
lblText.value = field.getLabel();
pbsi.childComponents.add(lblText);
Component.Apex.SelectList selList = new Component.Apex.SelectList(id = field.getName(), multiselect = false, size = 1, style = 'width:200px;');
if (dt == Schema.DisplayType.Integer || dt == Schema.DisplayType.Double || dt == Schema.DisplayType.Currency || dt == Schema.DisplayType.Percent)
{
AddSelectOption(selList, 'Keep highest value');
AddSelectOption(selList, 'Keep lowest value');
AddSelectOption(selList, 'Keep master value');
pbsi.childComponents.add(selList);
pbs.childComponents.add(pbsi);
}
}
}
return pbs;
}
private void AddSelectOption(Component.Apex.SelectList selList, string option)
{
Component.Apex.SelectOption selOption = new Component.Apex.SelectOption();
selOption.itemLabel = option;
selOption.itemValue = option;
selList.childComponents.add(selOption);
}
Many thanks in advance
just bind the value attribute of the dynamically created select list to a string property using expression syntax like:
String selectedValue {get; set;}
...
setList.expressions.value = '{!selectedValue}';
the property should then store the selected value just as it would in a static declarative definition of the select list.
You could try and use dynamic visualforce binding. I.E.
map<String,String> FieldToString {get; set;}
...
selList.expressions.value = '{!FieldToString[\'' + fieldname + '\']}';
However, I have never used dynamic visualforce bindings within dynamic visualforce so caveat emptor.

Update SqlCE database Linq-to-Sql

When I update the database, I have to hard code mapping of each property, because using attach results in exception. This does not seem too elegant. Is there some easier solution here I'm not aware of? My code is below, showing the "MapData" method I call for this purpose:
Btw, entity classes (here; Users) are autogenerated with SqlMetal.
Public Class UserDataService
Public Sub Save(ByVal user As Users)
Dim ctx As New TestDB(connection)
Dim id As Integer = user.Id
If user.Id = 0 Then
Insert(user)
Else
Dim q = (From n In ctx.Users Where n.Id = id Select n).Single
q.MapData(user)
For Each o In user.Orders
o.Save()
Next
' ctx.Users.Attach(user, q) ' Does not work
' ctx.Users.Attach(user, True) ' Does not work
End If
ctx.SubmitChanges()
ctx.Dispose()
End Sub
End Class
Partial Public Class Users
Public Sub MapData(ByVal row As Users)
Me.Name = row.Name
End Sub
End Class
EDIT1:
Exceptions:
ctx.Users.Attach(user, q)
Cannot add an entity with a key that is already in use.
ctx.Users.Attach(user, True)
An entity can only be attached as modified without original state if it declares a version member or does not have an update check policy.
EDIT2:
I tried to add a column timestamp, which I believe is supposed to satisfy the last mentioned exception. So I add the column shown here. This doesn't help, but perhaps I need to make some further settings for that to be effective?
This way should work:
ctx.Users.Attach(user)
ctx.Refresh(RefreshMode.KeepCurrentValues, user) 'this line is important
ctx.SubmitChanges()
This is my console test app(it works), in C# though:
class Program
{
public static void Main(string[] args)
{
var t = new Test();
Customer c = t.GetCustomer();
c.CompanyName = "X";
t.AttachCustomer(c);
}
class Test
{
public Customer GetCustomer()
{
Customer cust;
using(DataContext db = new DataContext())
{
cust = db.Customers.Where(x => x.CustomerID == "ALFKI").Single();
db.Dispose();
}
return cust;
}
public void AttachCustomer(Customer cx)
{
using (DataContext db = new DataContext())
{
db.Customers.Attach(cx);
db.Refresh(RefreshMode.KeepCurrentValues, cx);
db.SubmitChanges();
db.Dispose();
}
}
}
}