ModelBinder NHibernate Complex Object with Collections - asp.net-mvc-2

Below, on initial load (Edit), everything displays fine. However, when I POST (Action below), it looks like it goes and tries to grab the ProductAttribute models separately, with the Product's id, which promptly fails. How to keep my Binder implementation from trying to re-bind Collections as separate entities?
Thanks!
Model
public class Product {
virtual public long Id { get; set; }
virtual public string Name { get; set; }
private IList<ProductAttribute> _productAttributes;
public virtual IList<ProductAttribute> ProductAttributes {
get{
if(_productAttributes == null){
_productAttributes = new List<ProductAttribute>();
}
return _productAttributes;
}
set{
_productAttributes = value;
}
}
}
View
<%using (Html.BeginForm(new {id = Model.Id > 0 ? (long?)Model.Id : null})) {%>
<table class="form">
<% for(var i=0; i < Model.ProductAttributes.Count; i++){
var pa = Model.ProductAttributes[i]; %>
<tr>
<th><%: Model.ProductAttributes[i].Name %></th>
<td>
<%: Html.Hidden("ProductAttributes.Index", i) %>
<% if(pa.CanSpecifyValueDirectly()){ %>
<%: Html.TextBoxFor(model => model.ProductAttributes[i].Value) %>
<% } else { %>
<%: Html.DropDownListFor(model => model.ProductAttributes[i].Value, new SelectList(pa.MarketAttribute.AttributeLevels, "id", "Name", pa.AttributeLevel)) %>
<% } %>
</td>
</tr>
<input type="submit" value="Save" />
</table>
<%}%>
Controller
public ActionResult Edit(long id, Product product) {
ViewData.Model = product;
if (ModelState.IsValid) {
var results = product.Update();
ViewData["results"] = results;
if (!results.Error) {
return RedirectToAction("Show", new { id = id });
}
}
return View();
}
Binder
public class StackModelBinder : DefaultModelBinder {
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) {
var modelInterface = modelType.GetInterface("IModel");
if (modelInterface == null) {
return base.CreateModel(controllerContext, bindingContext, modelType);
}
var value = bindingContext.ValueProvider.GetValue("id").RawValue.ToString();
if (string.IsNullOrEmpty(value)) {
return base.CreateModel(controllerContext, bindingContext, modelType);
}
var id = Convert.ChangeType(value, typeof (long));
var assembly = Assembly.GetAssembly(modelType);
var dao = assembly.GetType(string.Concat(assembly.GetName().Name, ".core.GlobalDao`1[", modelType.FullName, "]"));
if (dao == null) {
return base.CreateModel(controllerContext, bindingContext, modelType);
}
var method = dao.GetMethod("Get");
return method.Invoke(null, new[] {id});
}
}

Typically, it is a bad idea to push entities through the model binder--they tend to be a bit too complex for it to handle, never mind the exciting stuff that goes on in modern ORMs, such as dynamic proxies, that can give the ModelBinder or the ORM fits.
Your best bet here is to change the rules and build a dedicated class for taking the edits and transferring that to the controller. This class can be ModelBinder-friendly and you get the added benefit of separating the UI from the domain entities.

Related

Insert Data to tblEducation using asp.net mvc 2.0

I have 2 tables in database:tbleducation,tblemployee.Anyway for table tblemployee, I have fields: employeeID,employeeName....I want to insert data into tbleducation that have fields such as:EmployeeID,Duration,.....And for the EmployeeID of table tbleducation I want to do the dropdownlist that list all EmployeeName in tblEmployee into dropdownlist.And I have code as below:
View
<div id="Education">
<%Html.EnableClientValidation(); %>
<% using (Html.BeginForm("Education","EmployeeProfile")){%>
<% Html.ValidationSummary(true, "Unsuccessfull"); %>
<table>
<tr>
<td>Employee Name</td>
<td><%= Html.DropDownListFor(x => x.EmployeeName, Model.Employee, "select EmployeeName")%</td>
<td>Duration</td>
<td><%: Html.TextBoxFor(m=> m.Duration, new { id="duration",name="duration"})%>
<%: Html.ValidationMessageFor(m => m.Duration) %></td>
<tr>
<td><input type="submit" name="add" id="add" value="Add" /></td>
</tr>
</table>
<%}%>
</div>
Model
public class UserModels
{
public string EmployeeName { get; set; }
public int EmployeeCode { get; set; }
public IEnumerable<SelectListItem> Employee { set; get; }
}
Controller
public ActionResult Education() {
var query = (from e in context.tblEmployee_Employee
select new
{
empID = e.Code,
EmpName = e.NameEng
}
).ToList();
var model = new UserModels();
var _Emp = query;
foreach (var item in _Emp)
{
model.EmployeeCode = item.empID;
model.EmployeeName = item.EmpName;
model.Employee = new SelectList(_Emp, "EmpName", "EmpName");
}
return View(model);
}
[HttpPost]
public ActionResult Education(UserModels model, FormCollection edu) {
tblEmployee_Education education = new tblEmployee_Education();
education.Duration = edu["Duration"].ToString();
education.Certificate = edu["Certificate"].ToString();
education.Country = edu["Country"].ToString();
education.SchoolName = edu["SchoolName"].ToString();
education.Major = edu["Major"].ToString();
education.SubDescript = edu["SubDescript"].ToString();
string EName = edu["EmployeeName"].ToString();
return Content(
string.Format(
"Selected role for {0} is {1}", model.EmployeeName, model.EmployeeCode
)
);
context.AddTotblEmployee_Education(education);
context.SaveChanges();
return View("Personal");
}
I got the warning message "unreachble code detected".I really do not know how to solve it.Please kindly help me,
Thanks in advanced,
In your POST action, here:
return Content(
string.Format(
"Selected role for {0} is {1}", model.EmployeeName, model.EmployeeCode
)
);
you have already returned a result and the action execution ends.
So get rid of the lines that follow it, they will never be executed:
context.AddTotblEmployee_Education(education);
context.SaveChanges();
return View("Personal");
And if you want them to be executed, then remove the return Content(...) line.

LINQ to Entities error when trying to bind a dropdownlist

I am trying to create a viewmodel and add a SelectListItem to allow me to bind a dropdown list.
I have a very basic viewmodel that looks like this
public class CreatePurchaseViewModel
{
public Product Product { get; set; }
public IEnumerable<SelectListItem> Products { get; set; }
public int SelectedProductId { get; set; }
public DateTime OrderDate { get; set; }
public bool OrderSent { get; set; }
}
My controller looks like this
[HttpGet]
public ActionResult Create()
{
var model = new CreatePurchaseViewModel
{
Products = context.Products.Select(x =>
new SelectListItem()
{
Text = x.ProductName,
Value = x.ProductID
})
};
return View(model);
}
However it complains that Value = x.Product cant convert type int to string. So if I add a .ToString it compiles ok but when I try load the view I get an error
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
My View
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>CreatePurchaseViewModel</legend>
<div class="editor-label">
#Html.LabelFor(model => model.SelectedProductId)
</div>
<div class="editor-field">
#Html.DropDownListFor(x => x.SelectedProductId,Model.Products)
#Html.ValidationMessageFor(model => model.SelectedProductId)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.OrderDate)
</div>
<div class="editor-field">
#Html.TextBoxFor(model=>model.OrderDate)
#Html.ValidationMessageFor(model => model.OrderDate)
</div>
<div>
Sent
#Html.RadioButtonFor(model => model.OrderSent, "Sent", new { #checked = true })
Not Sent
#Html.RadioButtonFor(model=>model.OrderSent,"Not Sent")
Im pretty new to both entity framework and mvc so any help would be great.
Thank you
You haven't specified how does your Product model look like but we can assume that the ProductID property is integer so you might need to convert it to a string:
[HttpGet]
public ActionResult Create()
{
var model = new CreatePurchaseViewModel
{
Products = context.Products.Select(x =>
new SelectListItem
{
Text = x.ProductName,
Value = x.ProductID.ToString()
}
)
};
return View(model);
}

ASP.NET MVC2: Update of a model for further modifications in a POST handler.

Model:
public class Model
{
public ItemType Type { get; set; }
public int Value { get; set; }
}
public enum ItemType { Type1, Type2 }
Controller:
public ActionResult Edit()
{
return View(new Model());
}
[HttpPost]
public ActionResult Edit(Model model, bool typeChanged = false)
{
if (typeChanged)
{
model.Value = 0; // I need to update model here and pass for further editing
return View(model);
}
return RedirectToAction("Index");
}
And of course View:
<div class="editor-label"><%: Html.LabelFor(model => model.Type) %></div>
<div class="editor-field">
<%: Html.DropDownListFor(
model => model.Type,
Enum.GetNames(typeof(MvcApplication1.Models.ItemType))
.Select(x => new SelectListItem { Text = x, Value = x }),
new { #onchange = "$(\'#typeChanged\').val(true); this.form.submit();" }
)
%>
<%: Html.Hidden("typeChanged") %>
</div>
<div class="editor-label"><%: Html.LabelFor(model => model.Value) %></div>
<div class="editor-field"><%: Html.TextBoxFor(model => model.Value) %></div>
<input type="submit" value="Create" onclick="$('#typeChanged').val(false); this.form.submit();" />
The code in controller (with the comment) doesn't work as I expect. How could I achieve the needed behavior?
As I wrote here multiple times, that's how HTML helpers work and it is by design: when generating the input they will first look at the POSTed value and only after that use the value from the model. This basically means that changes made to the model in the controller action will be completely ignored.
A possible workaround is to remove the value from the modelstate:
if (typeChanged)
{
ModelState.Remove("Value");
model.Value = 0; // I need to update model here and pass for futher editing
return View(model);
}

Why when I pass a Lambda expressin Func thats give me an error

When I pass call a Generic Method that return A Func and pass in parameter of the Where, that's dosen't work. (System.InvalidOperationException: Internal .NET Framework Data Provider error 1025.)
The error is when I want to get the Role information.
For the Role, I need to perform a Where Clause Expression EX: (p => p.LangID == 1)
This code dosen't Work
In the repository
public Func<T, bool> GetLmbLang<T>() where T:class,IBaseGenericTxt
{
int lang = -1;
lang = Convert.ToInt32(HttpContext.Current.Session["Language"]);
return (p => p.LangID == lang);
}
In the controller
var ViewModel = _db.Contacts.Where(a=> a.IsActive == true).Select(a => new ContactListViewModel {
ContactID = a.ContactID,
ContactName = a.ContactName,
Role = a.ContactType.ContactTypeTexts.Where(repGeneric.GetLmbLang<ContactTypeText>()).Select(af => af.Txt).FirstOrDefault(),
CompanyType = a.Supplier.SupplierName,
Addr = a.Address ,
Email = a.ContactEmail,
Phone = a.ContactPhone
}).ToList();
for (int i = 0; i < ViewModel.Count(); i++)
{
Response.Write(ViewModel.ElementAt(i).ContactID + "<br />");
}
This code WORK
int lang = -1;
lang = Convert.ToInt32(Session["Language"]);
var ViewModel = _db.Contacts.Where(a=> a.IsActive == true).Select(a => new ContactListViewModel {
ContactID = a.ContactID,
ContactName = a.ContactName,
Role = a.ContactType.ContactTypeTexts.Where(p => p.LangID == lang).Select(af => af.Txt).FirstOrDefault(),
CompanyType = a.Supplier.SupplierName,
Addr = a.Address ,
Email = a.ContactEmail,
Phone = a.ContactPhone
}).ToList();
for (int i = 0; i < ViewModel.Count(); i++)
{
Response.Write(ViewModel.ElementAt(i).ContactID + "<br />");
}
My ContactListViewModel
public class ContactListViewModel
{
public int ContactID { get; set; }
public string ContactName { get; set; }
public string Role { get; set; }
public string Company { get; set; }
public string CompanyType { get; set; }
public Address Addr { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
My List View
..... Inherits="System.Web.Mvc.ViewPage<List<mvcinfosite.ViewModels.ContactListViewModel>>" %>
<table class="genTable">
<% for (int i = 0;i < Model.Count; i++) { %>
<tr>
<td>
<%: Html.ActionLink(item.ContactName, "Edit", new { id=item.ContactID }) %>
</td>
<td>
<%: item.Role %>
</td>
<td>
<%: item.Company %>
</td>
<td>
<%: item.CompanyType %>
</td>
<td>
<%: GlobalHelper.GetAddress(item.Addr) %>
</td>
<td>
<%: item.Email %>
</td>
<td>
<%: item.Phone %>
</td>
</tr>
<% } %>
</table>
As naasking points out, you need to use an Expression of a Func instead of a straight Func:
public Expression<Func<T, bool>> GetLmbLang<T>() where T:class,IBaseGenericTxt
{
int lang = -1;
lang = Convert.ToInt32(HttpContext.Current.Session["Language"]);
return (p => p.LangID == lang);
}
Edit
Ah, yes, well the problem is that your function doesn't actually know what class it's working with at compile time: it only knows that it's a class, and it implements IBaseGenericTxt. So when you say p.LangId, that part of the expression is calling IBaseGenericTxt.LangId, and not ContactTypeText.LangId.
You'll need to build your own expression tree in order to get this to work right. Something like this:
var paramExpr = Expression.Parameter(typeof(T), "p");
return Expression.Lambda<Func<T, bool>>(
Expression.Equal(
Expression.Property(paramExpr, "LangId"),
Expression.Constant(lang)),
paramExpr);
Edit 2
Two things:
Because LINQ to Entities will try to take anything in a query expression and convert it to a SQL statement, you have to be careful not to go calling methods in the middle of your query. You'll want to call the GetLmbLang method first and store its value in a variable to use in the query.
As you point out in your comment, because the ContactTypeTexts property does not implement IQueryable, this gets particularly tricky. You have three options as far as I can tell:
Create your entire select statement as an expression tree. This is very annoying and error-prone.
Use Joe Albari's LinqKit to "Compile" and "Expand" your query. LinqKit will traverse the expression tree and build a new tree wherein your query Expression is converted to its equivalent Func.
Go back to your data context rather than using the ContactTypeTexts property.
Personally, I would probably go with the last option, like this:
var lambdaLang = repGeneric.GetLmbLang<ContactTypeText>();
var ViewModel = _db.Contacts
.Where(a=> a.IsActive == true)
.Select(a => new ContactListViewModel {
ContactID = a.ContactID,
ContactName = a.ContactName,
Role = _db.ContactTypeTexts
.Where(ct => ct.ContactType.Contacts.Any(
c => c.ContactId == a.ContactId)
.Where(lambdaLang)
.Select(af => af.Txt).FirstOrDefault(),
CompanyType = a.Supplier.SupplierName,
Addr = a.Address ,
Email = a.ContactEmail,
Phone = a.ContactPhone
}).ToList();
The latter code works because the C# compiler converts it into an expression tree, ie. System.Linq.Expression, whereas your original code was compiled as a Func. Linq to SQL as currently designed cannot process the Func, only expression trees.

Html.EditorFor does not render anything if I use a custom object?

It seems like it's not possible to edit custom object anymore after I upgraded to asp.net mvc 2 rc 2? I use this approach http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html with a custom object like this:
My model has just one property but inherits from an abstract base class
public class Page : ContentItem {
[DataType(DataType.MultilineText)]
public virtual string MainIntro { get;set; } // This property render correct
[DisplayFormat(NullDisplayText="(null value)")]
public virtual DetailCollection Tags { get; set; }
}
My controller looks like this
public ActionResult Edit(string pagePath) {
var page = _repository.GetByUrlSegment(pagePath);
return View(page.EditViewName, new DashboardModel(page, RootPages));
}
And my view looks like this
<% using (Html.BeginForm("update","Dashboard", FormMethod.Post, new { name = "editForm" } )) %>
<% { %>
<div>
<%=Html.EditorFor(model => model.CurrentItem) %>
<div class="editor-button">
<input type="submit" value="Save" />
</div>
</div>
<% } %>
Perhaps it would be better to expose this to the view as a space-separated string and exclude the collection from being displayed in the view. Alternatively, you might be able to define a specific template for how you want to display a collection. It's not clear to me how MVC would be able to determine what to display otherwise.
Try something like:
[ShowForDisplay(false)]
[ShowForEdit(false)]
public virtual DetailCollection Tags { get; set; }
public virtual string TagList
{
get
{
if (tags == null) return "(null value)";
// assumes DetailCollection implements IEnumerable<string>
return string.Join( " ", tags.Select( t => t).ToArray() );
}
set
{
tags = new DetailCollection( value.Split( new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries )
.Select( s => s.Trim() ) );
}
}