I tried to create more than one combobox, but model to view combobox is different. then I tried to combine two models in one class, but I get an error "object reference not set to an instance of an object" when I click on the link to go to that page.
please help me to fix the matter. thank you, sorry if I mess sentence.
This piece from some source code :
controller partial combobox ,
[ValidateInput(false)]
public ActionResult cbPartialCategoryDetail(string group)
{
SalesMonitoringDAC modelSM = new SalesMonitoringDAC();
MPMPRODUCTGROUPLINE item = new MPMPRODUCTGROUPLINE();
item.GROUPID = group;
List<string> mpmDetailCat = modelSM.GetProductGroupDetail(group);
return PartialView("_cbPartialCategoryDetail", mpmDetailCat);
}
and this is code combobox partial
#model DIS_iDealer.Models.SalesMonitoringModel
#Html.DevExpress().ComboBoxFor(m => m.mpmGroupLine.DESCRIPTION, settings =>
{
settings.Name = "Desc_ID_CB";
settings.Properties.IncrementalFilteringMode = IncrementalFilteringMode.Contains;
settings.Properties.DropDownStyle = DropDownStyle.DropDownList;
settings.CallbackRouteValues = new { Controller = "Report", Action = "cbPartialCategoryDetail" };
settings.Properties.CallbackPageSize = 50;
settings.Properties.ValueField = "DESCRIPTION";
settings.Properties.TextField = "DESCRIPTION";
settings.Properties.ValueType = typeof(string);
settings.Width = 100;
settings.Properties.DropDownWidth = 100;
settings.SelectedIndex = 0;
settings.Properties.ClientSideEvents.BeginCallback = "function(s, e) { e.customArgs['GroupId'] = Category_Id_CB.GetValue(); }";
settings.Properties.ClientSideEvents.SelectedIndexChanged = "OnInitDetailCategory";
settings.Properties.ClientSideEvents.Init = "OnInisialDetailCategory";
settings.Properties.ValidationSettings.ErrorTextPosition = ErrorTextPosition.Right;
settings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
settings.Properties.ValidationSettings.Display = Display.Dynamic;
settings.Enabled = true;
settings.ShowModelErrors = true;
//}).BindList(ViewBag.DetailCategory).GetHtml()
}).BindList((new DIS_iDealer.DataAccess.SalesMonitoringDAC()).GetProductGroupDetail(Model.mpmGroupLine.GROUPID)).GetHtml()
this is class two combine the two model
namespace DIS_iDealer.Models
{
public class SalesMonitoringModel
{
public MPMPRODUCTGROUPLINE mpmGroupLine { get; set; }
public MPMDataDealer mpmDataDealer { get; set; }
public SalesMonitoringModel(MPMPRODUCTGROUPLINE _mpmGroupLine, MPMDataDealer _mpmDataDealer) {
mpmGroupLine = _mpmGroupLine;
mpmDataDealer = _mpmDataDealer;
}
}
}
Isn't that really strange. In your action method you are returning a List<string> as seen below
List<string> mpmDetailCat = modelSM.GetProductGroupDetail(group);
return PartialView("_cbPartialCategoryDetail", mpmDetailCat);
Whereas in your partial view you are expecting a model of type SalesMonitoringModel and that's the source of this error. I am not sure what you are exactly trying to achieve and so can't help beyond this
#model DIS_iDealer.Models.SalesMonitoringModel
Related
Here is my code:
public List<DiscountVM> GetAllDiscounts(long CompanyID)
{
return _context.Discounts.Where(x => x.IsActive == true && x.CompanyID == CompanyID).Select(s => new DiscountVM()
{
DiscountID = s.DiscountID,
LocationID = s.LocationID,
CompanyID = s.CompanyID,
DiscountName = s.DiscountName,
DiscountValue = s.DiscountValue,
DiscountType = s.DiscountType,
ServiceSale = s.ServiceSale,
ProductSale = s.ProductSale,
VoucherSale = s.VoucherSale,
IsActive = s.IsActive,
AddDate = s.AddDate,
AddedByID = s.AddedByID,
UpdateDate = s.UpdateDate,
UpdatedByID = s.UpdatedByID,
}).ToList();
}
I want to convert Model to View model because if i am not converting then i have to fill all the values.
The code is working fine if I fill all the values. I trying to find a way in which I can easily convert the model to view model then there would be no need to fill all the values.
well this can be done some different way but easiest way for you that change you view model a little bit by adding you db model as property of your view model like this
public class DiscountVM
{
public Discounts DiscountsList{ get; set; }
// Other property
}
and return like this
public List<DiscountVM> GetAllDiscounts(long CompanyID)
{
return _context.Discounts.Where(x => x.IsActive == true && x.CompanyID ==CompanyID).Select(s => new DiscountVM()
{
DiscountsList=s
}).ToList();
}
Have you looked into AutoMapper to help you with that?
What can be simplest way for having rendered in a view information from multiple models. I use ViewModel in some scenarios (in particular when models are not related directly), but now I want to made a kind of dashboard for the current user. So apart from AspNetUsers model I have for example several models (e.g. Orders, OperationJournal, Jobs etc.) that in terms of entity have each a foreign key on UserID.
I made a ViewModel such:
namespace JobShop.Models
{
class QuickProfileVM
{
public IEnumerable<Jobs> Jobs { get; set; }
public IEnumerable<AspNetUsers> AspNetUsers { get; set; }
public IEnumerable<CreditJournal> CreditJournal { get; set; }
public IEnumerable<CandidateReview> CandidateReview { get; set; }
}
}
(since the base models that I need, are done by EF they have all about relations between entities) but it seems to me that is not enough. I am not able to view both the current user profile (so one record) and it's details (more than one record and more than one model).
I have try with Partial View, both with own controller or with actions in Dashboard View controller.
As an example an ActionResult that now I play with:
public ActionResult QuickProfile()
{
var QuickProfile = new QuickProfileVM();
var AspNetUsers = new AspNetUsers();
if (User.Identity.IsAuthenticated)
{
var CurrentUser = User.Identity.GetUserId();//UserManager.FindById(User.Identity.GetUserId());
var TheUser = db.AspNetUsers.Where(u => u.Id == CurrentUser)
.Select(u => new
{
ID = u.Id,
Email = u.Email,
PhoneNumber = u.PhoneNumber,
Companyname = u.Companyname,
Address = u.Address,
ZIP = u.ZIP,
City = u.City,
Country = u.Country,
Website = u.Website,
Facebook = u.Facebook,
Twitter = u.Twitter,
GooglePlus = u.GooglePlus,
Dribble = u.Dribble,
BirthDate = u.BirthDate,
Username = u.UserName,
Surrname = u.Surname,
Name = u.Name,
Role = u.Role,
ThumbURL = u.ThumbURL,
CreditBalance = u.CreditBalance
}).Single();
var TheJournal = db.CreditJournal.Where(tj => tj.UseBy == CurrentUser)
.Select(tj => new
{
IdJournal = tj.IdJournal,
Operation = tj.Operation,
CvID = tj.CvID,
JobID = tj.JobID,
CreditConsumed = tj.CreditConsumed,
UseDate = tj.UseDate,
UseBy = tj.UseBy
}).ToList();
//similar for Jobs and CandidateReview
//
var UserId = TheUser.ID;
var username = TheUser.Username;
var role = TheUser.Role;
var InitialCredit = TheUser.CreditBalance;
AspNetUsers.UserName = TheUser.Username;
AspNetUsers.Companyname = TheUser.Companyname;
AspNetUsers.Surname = TheUser.Surrname;
AspNetUsers.Name = TheUser.Name;
AspNetUsers.ThumbURL = TheUser.ThumbURL;
AspNetUsers.CreditBalance = InitialCredit;
//I put this to ilustrates what I have accesible for example
//about CreditJournal: only methods, not properties
QuickProfile.CreditJournal.AsEnumerable();
var id = CurrentUser;
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
AspNetUsers aspNetUsers = db.AspNetUsers.Find(id);
if (aspNetUsers == null)
{
return HttpNotFound();
}
}
return View(AspNetUsers);
//Disbled since at this stage is not usefull
//return View(QuickProfile);
//return View();
}
I suggest you consider using Html.RenderAction in your view. For example, say your main dashboard is this:
#{
ViewBag.Title = "title";
}
<h2>Multiple Models</h2>
#{ Html.RenderAction("GetData", "Foo"); }
You can use Html.RenderAction to call FooController.GetData()
public class FooController : Controller
{
public ActionResult GetData()
{
var viewModel = new FooViewModel();
viewModel.TimeStamp = DateTime.UtcNow;
return View(viewModel);
}
}
So rather than having one viewmodel with lots of other viewmodels attached as properties, you can split up the rendering of the dashboard view.
Overall this should makes things easier for you - I've used this approach in the past and have found it reduces complexity.
Framework: ASP.NET MVC 3
Tools: VS 2012
Hi guys,
I'm new to ASP.NET mvc, I have been using strongly typed html helper for some time,I don't understand them completely but know that it can provide type checking and keeps name according to property name,after using it for many times I faced this error constantly and dont seem to have any hint why its occuring tried several google searches but none helped me.
Error:
The name 'm' does not exist in the current context
Code:
#Html.LabelFor(m => m.OrginalCont.Title)
But this line of code works perfectly fine.
#Html.TextBox("origTitle", Model.OrginalCont.Title, new { #readonly = "", #class = "text-rounded", style = "width:425px" })
Can someone tell me whats going on here.I have used the same m => m.Name syntax several times in view but they are all working fine.
Update:
Alright I will try to post more code,but as the code may get too long I will try to keep it short and will post code which is related to the problem.
View
#model TS.MOP.Interface.Models.ProjectTranslationModel
<div class="form-item">
#Html.LabelFor(model => model.OrginalCont.Title)
<div class="editor-field">
#Html.TextBox("origTitle", Model.OrginalCont.Title, new { #readonly = "", #class = "text-rounded", style = "width:425px" })
</div>
</div>
Model
public class ProjectTranslationModel
{
public int ProjectId { get; set; }
[Required(ErrorMessageResourceType = typeof(Names), ErrorMessageResourceName = "Required_ErrorMessage")]
[DataType(DataType.Text)]
[Display(ResourceType = typeof(Names), Description = "TranslateLangs_Description", Name = "TranslateLangs_Title")]
public List<SelectListItem> TranslatableLangSelect { get; set; }
public TranslatableLanguageEditor TranslatableLangEditor { get; set; }
public OriginalContent OrginalCont { get; set; }
}
Controller
[Authorize]
public ActionResult TranslationEdit(int? id)
{
if (id.HasValue && User.IsInRole("ContentTranslator"))
{
var model = new ProjectTranslationModel();
model.ProjectId = id.Value;
var ContentTypeId = 2;//Project Page
Guid _currentUserId = Guid.Parse(Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString());
var translatorSettings = db.TranslatorSettings.Where(x => x.UserId == _currentUserId).ToList();
model.TranslatableLangEditor = new TranslatableLanguageEditor();
var list = new List<SelectListItem>();
//Original Text
var originalContent = db.Projects.Where(x => x.Id == id.Value)
.Select(d => new OriginalContent { Title = d.Title, Content = d.Description, ContentCultureId = d.ProjectType.ContentCultureId }).FirstOrDefault();
model.OrginalCont = originalContent;
foreach (var item in translatorSettings)
{
//Select List
var li = new SelectListItem();
if (item.ContentCultureId != originalContent.ContentCultureId)//Dont add the Original Content CultureId to the dropDown
{
li.Value = item.ContentCultureId.ToString();
li.Text = item.ContentCulture.Description;
list.Add(li);
}
}
model.TranslatableLangSelect = list;
return View(model);
}
else
{
return View("Index");
}
}
I have written following code for datagrid.
Controller:
public class EmployeeController : BaseController
{
public static List<EmployeeModel> Employees { get; set; }
public ActionResult EmployeeSearchPartial()
{
return PartialView("EmployeeSearchPartial", Employees);
}
}
EmployeeSearch partial view
#Html.DevExpress().GridView(
settings =>
{
settings.Name = "gvEmployeeSearch";
settings.KeyFieldName = "EmployeeId";
settings.SettingsPopup.EditForm.Width = 600;
settings.CallbackRouteValues = new { Controller = "Employee", Action = "EmployeeSearchPartial" };
settings.Width = 720;
settings.Height = 600;
settings.SettingsPager.PageSize = 15;
settings.Settings.VerticalScrollBarStyle = DevExpress.Web.ASPxGridView.GridViewVerticalScrollBarStyle.Standard;
settings.Settings.VerticalScrollableHeight = 350;
settings.ControlStyle.Paddings.Padding = System.Web.UI.WebControls.Unit.Pixel(0);
settings.ControlStyle.Border.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(0);
settings.ControlStyle.BorderBottom.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(1);
settings.SettingsText.EmptyDataRow = "";
settings.SettingsBehavior.AllowFocusedRow = true;
settings.ClientSideEvents.RowClick = "OnCellClick";
settings.Columns.Add(column =>
{
column.Caption = "Employee Name";
column.FieldName = "Name";
column.Width = 420;
});
settings.Columns.Add(column =>
{
column.Caption = "Employee Number";
column.FieldName = "EmployeeId";
column.Width = 300;
});
}).Bind(Model).GetHtml()
Now in the above code, the grid uses callback functions for sorting, filtering any other functionality. When the grid calls a callback function, a new object gets created for EmployeeController so as to maintain employee datascrourse (Employees). We have to create a static variable in the controller to maintain the grid datascrouse list. It is ok for a single user, but when multiple users access that page then the grid value gets a conflict because MVC is a web application so multiple users can access same page at the same time.
Please share any solution to maintain the grid view data for each call back function.
I have created an entity data model from my database. however in certain areas of the application i need to pass two models. thus i create a third model which has as properties the objects of each required model.
In the scenario, i want to use one model just to show some data to the user and the other is to be populated by the user using form elements. therefore, i create a constructor in my custom model to populate it. here's the code:
THE CUSTOM MODEL
public class ordersModel
{
public ordersModel(order or)
{
this.prods = new order_products();
this.new_order = new order();
this.new_order.customer_id = or.customer_id;
this.new_order.my_id = or.my_id;
this.new_order.my_order_id = or.my_order_id;
this.new_order.order_date = or.order_date;
this.new_order.order_status_id = or.order_status_id;
}
public order new_order { get; set; }
public order_products prods { get; set; }
}
IT IS USED IN THE CONTROLLER AS FOLLOWS:
public ActionResult Create()
{
order or = new order();
// Store logged-in user's company id in Session
//or.my_id = Session["my_id"].ToString();
//do something to allow user to select customer, maybe use ajax
or.customer_id = "123";
or.order_amount = 0;
or.my_id = "74973f59-1f6c-4f4c-b013-809fa607cad5";
// display date picker to select date
or.order_date = DateTime.Now.Date;
// fetch statuses from database and show in select list box
or.order_status_id = 1;
return View(or);
}
//
// POST: /Orders/Create
[HttpPost]
public ActionResult Create(order or)
{
using (invoicrEntities db = new invoicrEntities())
{
var temp = db.last_order_number.SingleOrDefault(p => p.my_id == or.my_id);
if (temp != null)
{
or.my_order_id = temp.my_order_id + 1;
if (ModelState.IsValid)
{
ordersModel ord = new ordersModel(or);
db.orders.AddObject(or);
temp.my_order_id = temp.my_order_id + 1;
//TempData["my_order_id"] = or.my_order_id;
db.SaveChanges();
return RedirectToAction("AddProducts", ord);
//return RedirectToAction("AddProducts", new { id = or.my_order_id });
}
return View(or);
}
return RedirectToAction("someErrorPageDueToCreateOrder");
}
}
public ActionResult AddProducts()
{
using (invoicrEntities db = new invoicrEntities())
{
//string my_id = TempData["my_id"].ToString();
//string my_order_id = TempData["my_order_id"].ToString();
string my_id = "74973f59-1f6c-4f4c-b013-809fa607cad5";
int my_order_id = 1;
//Int64 my_order_id = Convert.ToInt64(RouteData.Values["order_id"]);
// Display this list in the view
var prods = db.order_products.Where(p => p.my_id == my_id).Where(p => p.my_order_id == my_order_id).ToList();
var or = db.orders.Where(p => p.my_id == my_id).Where(p => p.my_order_id == my_order_id).ToList();
if (or.Count == 1)
{
//ViewData["name"] = "sameer";
ViewData["products_in_list"] = prods;
ViewData["order"] = or[0];
return View();
}
return RedirectToAction("someErrorPageDueToAddProducts");
}
}
[HttpPost]
public ActionResult AddProducts(order_products prod)
{
prod.my_id = "74973f59-1f6c-4f4c-b013-809fa607cad5";
// find a way to get the my_order_id
prod.my_order_id = 1;
return View();
}
THIS ALL WORKS OUT WELL, UNTIL IN THE "ADDPRODUCTS" VIEW:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<invoicr.Models.ordersModel>" %>
AddProducts
<h2>AddProducts</h2>
<%: Model.new_order.my_id %>
the above statement gives an error
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
what am i doing wrong here?
You seem to be passing the wrong model when redisplaying your Create view.
Try passing the ord instance which is of type ordersModel and which is what your view is strongly typed to:
public ActionResult Create(order or)
{
using (invoicrEntities db = new invoicrEntities())
{
var temp = db.last_order_number.SingleOrDefault(p => p.my_id == or.my_id);
if (temp != null)
{
or.my_order_id = temp.my_order_id + 1;
ordersModel ord = new ordersModel(or);
if (ModelState.IsValid)
{
db.orders.AddObject(or);
temp.my_order_id = temp.my_order_id + 1;
db.SaveChanges();
return RedirectToAction("AddProducts", ord);
}
return View(ord);
}
return RedirectToAction("someErrorPageDueToCreateOrder");
}
}
UPDATE:
Now that you have shown your AddProducts action you are not passing any model to the view although your view expects an ordersModel instance. So don't just return View();. You need to pass an instance of ordersModel:
if (or.Count == 1)
{
ViewData["products_in_list"] = prods;
ViewData["order"] = or[0];
ordersModel ord = new ordersModel(or[0]);
return View(ord);
}