displaying data related to a contact - vb.net

I don't really know how to deal with the next issue :
So, I've a webbased application developped with ASP.NET MVC3, which is used to remember when an event is relayed to some people.
I've 3 tables
Contact
id_contact
name
firstname
...
event
id_event
...
transmission
FK_id_event
FK_id_firstname
mailed (boolean)
phoned (boolean)
For each event, I need to list all the contacts that are related to this event. And for each contact, I need to display 2 checkboxes that have to be checked if the contact has been called or mailed.
'
' GET: /Event/Details/5
Function Details(id As Integer) As ViewResult
Dim event As event = db.event.Single(Function(o) o.idOpportunite = id)
Dim contacts = (From a In db.contact, b In db.transmission
Where a.id_Contact = b.FK_id_contact And b.FK_id_event = id
Select a)
Dim transmission = (From a In contacts, b In db.transmission
Where a.id_Contact = b.FK_trans_cont
Select b)
Dim model = New EventDetails With {
.event= event,
.Contacts = contacts,
.TransOpp = transopp
}
Return View(model)
End Function
I don't know if the "transmission" part of the code is good or not.
Here in the view, this is were I display the contacts
#For Each contact In Model.contacts
#<tr>
<td>
#Html.ActionLink(contact.name + " " + contact.firstname , "Details", New With {.id = contact.idContact})
</td>
<td>
#Html.Raw(contact.phone)
</td>
<td>
#*Html.DisplayFor(Function(modelItem) currentItem.mail)*#
<a href=mailto:#contact.mail>#Html.Raw(contact.mail)</a>
</td>
<td>
***My checkboxes should be here***
</td>
</tr>
Next
So, my question is, what should I do to display those checkboxes?
(sorry if I'm not understandable, I'm not a native english speaker. Don't hesitate to edit my english mistakes (or the title which is not a great one)).
With the help of Yasser, I've done this :
#code
Dim mail As Boolean = (From a In Model.Event
Where a.FK_id_contact = contact.idContact And a.FK_id_event = Model.Opportunite.idOpportunite
Select a.mailed)
End Code
However, I get an error :
Value of type 'System.Collections.Generic.IEnumerable(Of Boolean)' cannot be converted to 'Boolean'.

Here is something that should help
#{
bool isMailed = // set values;
bool isPhoned = // set values
}
#Html.CheckBox("mailed ", isMailed );
#Html.CheckBox("phoned", isPhoned );

In your ContactViewModel you can have two properties
bool isMailed ;
bool isPhoned ;
Then you can query the database from your controller before you bind the viewmodel the view and set those parameters. For example if you are showing data for contact id = 1 and event id = 2, you can query the database table transmissions and find whether you have called or mailed before and update the variable in ContactViewModel.
then in your view you can bind the values to the checkbox as follows
#Html.CheckBox("mailed ", contact.isMailed );
#Html.CheckBox("phoned", contact.isPhoned );
if you want to update the mailed or phoned in the database you can do it using the above ViewModel by submitting data to the controller. from controller you can find what is the Event_Id, Contact_Id and mailed or phoned , then you can update the database accordingly

Related

How to assign null/string to an int type in MVC Razor using Entity Framework?

I am using a simple MVC 4 application using Entity Framework.
In my View I am displaying data from of a table using webgrid.
View Also has Textboxes(EditorFor) for saving any new record in the table.
I am using partial view for the Textboxes, as in the beginning when the page is launched, the textboxes should remain empty.
Out of 5, two columns are of integer types.
In order to make the textboxes empty initially I am using a new object as -
#if (!dataGrid.HasSelection)
{
Datamodel = new EntityFrDemo.Models.FacultyDetails { DepartmentID = 0, Name = "", Subject = "", YrsExp = 0, Email = "" };
Html.RenderPartial("~/Views/Shared/_FacultyDetails.cshtml", Datamodel);
}
//------------------------------------------------------------------------
#Html.LabelFor(model => model.DepartmentID)
#Html.EditorFor(model => model.DepartmentID)
#Html.ValidationMessageFor(model => model.DepartmentID)
//-----------------------------------------------------------------------------
So I am able to make my boxes empty, however for the Integer type boxes '0' is coming, as I can only assign zero.
So How can I override/superimpose the integer value type boxes to empty string type so that boxes remains empty only in case when no row is selected i.e. in initial stage...?
When you use #Html.EditorFor() with a int value, Razor generate a html tag like this
<input type="number" name="propertyName" id="propertyName" value="propertyValue" />
If you didn't set a value for the int property, the default int value is zero. To set another value in the html tag, you can write it without Razor or you can set the value like the code below.
#Html.EditorFor(model => model.DepartmentID, new { htmlAttributes = new { #Value = "" } })
Note: It is capital "V", not a lower case "v".

VB MVC Join Tables Return View

I really have no idea.
I am trying to return a view with results from a Joined Table.
I keep receiving this error:
"The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[System.String[]]', but this dictionary requires a model item of type 'tacticMVC.crypto'. "
my controller code is as follows
Function Details(Optional ByVal id As Integer = Nothing) As ActionResult
If Request.IsAuthenticated = True Then
'Dim cryptoes = db.cryptoes.Where(Function(c) c.key_email = HttpContext.User.Identity.Name()).ToList()
'Dim crypto As crypto = db.cryptoes.Find(cryptoes(0).id_cypto)
Dim crypto = (From c In db.cryptoes
Join con In db.consumers On c.id_consumer Equals con.id_consumer
Where c.id_consumer = 3
Select {c.key_email})
Return View(crypto)
Else
Return HttpNotFound()
End If
End Function
If I use just the 2 commented lines in the function the view returns fine but of course I can only get the data from one table. I need to join the tables and then return the data.
I've tried adding .toList(), .singleordefault() etc - does not solve anything
the vbhtml file:
#ModelType tacticMVC.crypto
<h2>Details</h2>
#Using Html.BeginForm("Action", "Controller", FormMethod.Post)
#<fieldset>
<div class="display-label">
#Html.DisplayNameFor(Function(model) model.key_email)
</div>
<div class="display-field">
#Html.DisplayFor(Function(model) model.key_email)
something
</div>
</fieldset>
#<p>
#Html.ActionLink("Edit", "Edit", New With {.id = Model.id_cypto}) |
#Html.ActionLink("Back to List", "Index")
</p>
End Using
You're returning a database query object directly, but your page's cshtml says it expects a typed ViewModel.
Where is tacticMVC.crypto defined and what does it look like?
Dim crypto does not mean "an object of type crypto", it means "A late-bound object named crypto".
Great! - Just like a lot of other experiences after spending hours on it I have worked it out immediately after I posted my question.
Answer:
Controller
Function Details(Optional ByVal id As Integer = Nothing) As ActionResult
If Request.IsAuthenticated = True Then
Dim cryptoes = db.cryptoes.Where(Function(c) c.key_email = HttpContext.User.Identity.Name()).ToList()
Dim crypto As crypto = db.cryptoes.Find(cryptoes(0).id_cypto)
Return View(crypto)
Else
Return HttpNotFound()
End If
End Function
vbhtml
<div class="display-field">
#Html.DisplayFor(Function(model) model.consumer.name_consumer)
</div>
I am open to a better solution
Thanks

Using a list with MVC radiobuttonfor

I am using radiobuttonfor in MVC 4 and I am trying to give the user a selection of items that he can select. My problem is when one of the items is selected and it's posted to the controller, the string value is a .net system.guid rather than the actual guid value.
#foreach (var person in Model.InterviewDates[i].Interviewers) // all interviewers - not filtered
{
var list = Model.InterviewDates[i].Interviewers.Select(p => p.InterviewerID).ToList();
#Html.Raw(person.InterviewerName + " ")
// TODO: Lambda for getting all Chairs based on interview type and interview date
#Html.RadioButtonFor(m => m.InterviewSchedules[i].Chair, list, new { #style = "width:auto;background:none;border:none" })
<br />
}
I have a list of people and it contains their user ids. I then want the user to select a radio button to bind that value to the model.
- list Count = 3 System.Collections.Generic.List<System.Guid>
+ [0] {5fb7097c-335c-4d07-b4fd-000004e2d221} System.Guid
+ [1] {5fb7097c-335c-4d07-b4fd-000004e2d222} System.Guid
+ [2] {5fb7097c-335c-4d07-b4fd-000004e2d223} System.Guid
+ Raw View
When I post it goes here:
[HttpPost]
public ActionResult SubmittedInterviews(InterviewManagement InterviewManagement)
{
return View();
}
This is what I can see in quick watch
- InterviewManagement {IMecheAdmin.Models.InterviewManagement} IMecheAdmin.Models.InterviewManagement
+ InterviewDates Count = 0 System.Collections.Generic.List<IMecheAdmin.Models.InterviewDate>
- InterviewSchedules Count = 1 System.Collections.Generic.List<IMecheAdmin.Models.InterviewSchedule>
- [0] {IMecheAdmin.Models.InterviewSchedule} IMecheAdmin.Models.InterviewSchedule
Chair "System.Collections.Generic.List`1[System.Guid]" string
Cofacilitator null string
Facilitator null string
+ InterviewDate {01/01/0001 00:00:00} System.DateTime
Location null string
Observer null string
Preference false bool
Site null string
+ Raw View
+ InterviewTypes Count = 0 System.Collections.Generic.List<IMecheAdmin.Models.InterviewType>
SelectedMembershipType null string
And this is not what I want:
Chair "System.Collections.Generic.List`1[System.Guid]" string
Doesn't say actual GUID. Any ideas?
Radio button is normally used where we want user to only select only one option from the provided item.
So, you need to pass single object instead of list:
foreach(var item in list)
{
#Html.RadioButtonFor(m => m.InterviewSchedules[i].Chair,
item.InterviewerID,
new { #style = "width:auto;background:none;border:none" })
}

Select a default value in dropdownlistfor MVC 4

I'm trying to make a dropdownlistfor with a selected value but it doesn't work :/ And I search on the web but I don't find the solution :/
For the moment, I'm doing this :
In C# :
ViewBag.ID_VEH = new SelectList(db.VEHI, "ID_VEH", "COD_VEH", 4); // 4 is an example
In my cshtml :
#Html.DropDownListFor(model => model.ID_VEH, ViewBag.ID_VEH as SelectList)
The dropdownlist is well complete but the default value is not selected :/ do you have an idea please ?
What I like to do is add a list of the items to display in the dropdownlist to my model, so I don't have to pass that list via a viewbag.
Also i like to add a field to my model that contains the SelectedValue, that I fill in the controller
Then you can do
#Html.DropDownListFor(model => model.ID_VEH, new SelectList(Model.listVEH, "ID_VEH", "COD_VEH", Model.SelectedVEH_ID), "--Select VEH--")
just set the initial value of model.ID_VEH to 4:
In the controller:
model.ID_VEH = 4;
Just in case someone has similar problems finding the answer:
I want to have view with the dropdown boxes have focus on the items i give (hardcoded) in the controller:
Controller:
SGLDataRegistration.Models.DataRegistrationModel mdl = rwd.GetData(DateTime.Now.Year, currentWeek, DateTime.Now, 139, 1);
View:
<div id="tempCustomerselect">
#Html.LabelFor(m => m.CustomerName)
#Html.DropDownListFor(m => m.PitchID, new SelectList((new SGLDataRegistration.Models.CustomerModel().GetRoles()).OrderBy(x => x.CustomerName), "PitchID", "CustomerName"), new {id = "ddlCustomer", #class="jsddlCustomer"})
</div>
In this GetData, i setthe desired values hardcoded:
public SGLDataRegistration.Models.DataRegistrationModel GetData(int year, int weekNumber, DateTime datum, int pitchID, int parameter)
{
try
{
DataRegistrationParameters drp = GetParameter(parameter);
//vul een instantie van het dataregistrationmodel
SGLDataRegistration.Models.DataRegistrationModel drm = new Models.DataRegistrationModel();
drm.WeekNumber = weekNumber;
drm.BeginDay = datum;
drm.Parameter = parameter;
drm.Year = year;
drm.PitchID = pitchID;

don't know how to display data into the view (beginner)

Good Morning everybody.
I'm a beginner in .Net languages and need an example to be able to go further.
So, my objective is to display dates and comments from a datatable, below general information about a client.
The view needs to become something like that :
name firstname :
adress :
phone number :
...
date1
comment1
date2
comment2
...
It was easy to automatically generate a strongly typed view with the general data. Now, I don't get how to display the comments below.
Here is what I've already done into the controller
' GET: /Contacts/Details/5
Function Details(id As Integer) As ActionResult
Dim contact As contact = db.contact.Single(Function(c) c.idContact = id)
Dim listMeet = New List(Of meeting)
listMeet = (From d In db.meeting
Where d.FK_meet_contact = id
Select d).ToList()
ViewBag.listeMeeting = listMeet
Return View(contact)
End Function
Into the view, I dis plenty of wrong things... Let's show you the last one :
#ModelType MvcApplication4.contact
#Code
ViewData("Title") = "Details"
Dim list As List(Of Object) = ViewBag.listeMeeting
Dim ligne As ListItemCollection
End Code
[...]
<fieldset>
<legend><button onclick="togglefield('Meet')">Meetings</button></legend>
<div class="Meet">
#For Each ligne In ViewBag.listeMeeting
#Html.Raw(ViewBag.listeMeeting)
Next (ligne)
</div>
</fieldset>
What haven't I well understood?
ps : I'm not a native english speaker, so, sorry if my english sucks
You could use a view model instead of ViewBag:
Public Class MyViewModel
Public Property ContactDetails As Contact
Public Property Meetings As IEnumerable(Of Meeting)
End Class
and then populate this view model in your controller and pass to the view for displaying:
Function Details(id As Integer) As ActionResult
Dim contact As contact = db.contact.Single(Function(c) c.idContact = id)
Dim meetings =
(From d In db.meeting
Where d.FK_meet_contact = id
Select d).ToList()
Dim model = New MyViewModel With {
.ContactDetails = contact,
.Meetings = meetings
}
Return View(model)
End Function
and then have your view strongly typed to the view model:
#ModelType AppName.MyViewModel
<h2>#Model.ContactDetails.SomePropertyOfContact</h2>
<fieldset>
<legend>
<button onclick="togglefield('Meet')">Meetings</button>
</legend>
<div class="Meet">
#For Each meeting In Model.Meetings
#meeting.SomePropertyOfMeeting
Next
</div>
</fieldset>