I am using entity framework 4 and Csharp. I have a datagridview in a windows form. The datagridview has a colomn with a combobox. The combobox is to hold an Entity object.
Usually I do the following:
dataGridViewComboboxColumn.DisplayMember = "DisplayThis"
dataGridViewComboboxColumn.ValueMember = "DisplayThisId"
But I want:
dataGridViewComboboxColumn.ValueMember = Entity
Thanks!
I figured it out...I needed to add foreign key properties to my entity in the model.edmx.
Related
I am experimenting with the Entity Framework in a new mvc project, so I created a database, and started out with a database first approach. One of the tables I created had a foreign key to another table, and when the model got created, a virtual property was created to address the key value.
Then I had Visual studio create the controller / views with all the crud. Everything is working fine, but I want to change the dropdowns to Kendo.
The Controller is using ViewBag properties to send the foreign key data back to the view like so:
ViewBag.CourtId = new SelectList(db.Courts, "Id", "Name", tournament.CourtId);
the dropdown looks like this:
#Html.DropDownList("ProviderId", null, new {#class = "form-control"})<br />
I can't figure out how the viewBag data is being bound to the dropdown, nor have I been able to figure out how to substitiute a kendo dropdownlist?
How is this ViewBag data being bound to the dropdown?
so, I found the answer here: Click this Link
Just sub out:
#Html.DropDownList("ProviderId", "Select a Value")
with
#(Html.Kendo().DropDownListFor(model => model.ProviderId)
.OptionLabel("Select a Value")
.BindTo(ViewData["ProviderId"] as SelectList))
I have mvc view that has some columns from entity. When I do db.SaveChanges(), all columns that are not part of the view are being updated with NULL, so overwriting any values that were present in database record. That's very lame.
I am aware that I could do ModelView for the the view and bind just those columns that i want. But I am looking for a way to simply tell EF, to 'ignore' columns during this particular update, to NOT update columns that are NOT present in the MvC View.
I am using EF 5. Any suggestions?
I don't know why you want to avoid creating a separate Model but i guess you know what you are doing so maybe try this approach to trick EF into thinking the properties have not changed:
var entry = context.Entry(obj);
entry.Property(name).IsModified = false;
I haven't tried it myself but it should be possible in EF 5. If it doesn't work try accessing the property entry by searching the entry.CurrentValues.PropertyNames collection and then setting the IsModified to false.
I think what i needed is this
User u = db.Users.Find(user.UserID);
if (u!=null) {
TryUpdateModel(u);
if (ModelState.IsValid)
{
db.SaveChanges();
}
I am migrating Telerik ASP.net MVC to Kendo ASP.net MVC. In Telerik MVC I bounded column with an object in array and specified object type in Bound call and all worked perfectly.
columns.Bound(field.EntityFieldType, field.EntityFieldName).Title(field.DisplayName).Template(g =>
{<%= g.GetValue(field.EntityFieldId)%>}).Width(field.Width);
.......
But using Kendo MemberType is ignored and remains null. Is there a different way how I can set a MemberType in Kendo? Or if Kendo uses only Data Schema for column data types then how I can update schema in runtime?
It seems that Telerik is deprecating usage of Type in Bound call and uses DataSource Schema Model instead. I solved my problem by clearing Model Schema before I bound any columns.
columns.Container.DataSource.Schema.Model.Fields.Clear();
And then after each bound column I added a new entry in Model Field List. As my field names contains dot (.) I need to put Field Name in Quotation marks, so that generate JSon is correct.
ModelFieldDescriptor modelField = new ModelFieldDescriptor();
modelField.Member = "\"" + field.EntityFieldName + "\"";
modelField.MemberType = field.EntityFieldType;
modelField.DefaultValue = null;
modelField.IsEditable = false;
columns.Container.DataSource.Schema.Model.Fields.Add(modelField);
I am using Entity Framework Database First approach in ASP.NET MVC.
I have created models from an existing database and everything works fine.
I am using jqGrid and trying to create a drop down list for a column which is defined as an Int in the database.
In the database, the values are either 0 or 1, so in the dropdown list I will have to show two values such as "Import" or "Export" based on whether it is 0 or 1.
Would I be able to handle this scenario in the jqGrid?
Please post any suggestions if you have!
I think you can handle this thing in colModal itself. there's is one option editOption you can specify with you colModal and based on the value you are getting from data you can specfy the text property of that column
In this case, the data for the grid should contain “One” or “Two” to be set in the column myname.
Now, with this setting
<script>
jQuery("#grid_id").jqGrid({
...
colModel : [ {name:'myname', edittype:'select', formatter:'select', editoptions:{value:"1:One;2:Two"}} ... ]
...
});
</script>
the data should contain the keys (“1” or “2”), but the value (“One”, or “Two”) will be displayed in the grid.
check this link for further help. You will get your answer. Because you didn't post your code(atleast you should have done that for ur that particular column) i can not say you are implementing the same way.
I am trying to understand how associated entities are updated when using custom modelbinders.
If you have a Product entity with a relationship to a Category entity and you are displaying a list of category choice for a product in a dropdown on a form.
The the user assigns a new category and that change needs to be persisted with the Product. How is the binding implemented to assign the updated category? The properties f the Product are easy enough, but how do you set the Product.Category = category?
Hope that is clear :-)
Sounds like your custom model binding is there, you're just trying to setup up your relationship between your Product and Category.
To do this you would do something like this:
product.CategoryReference.EntityKey =
new EntityKey("Context.Category", "ID", categoryID);
That will simply update the foreign key relationship in your entity.