Ext.Net MVC refresh Panelgrid from controller - asp.net-mvc

In my view have this:
#{
var properties = db.StylesPropertyDefs.OrderBy(o => o.PropertyId);
}
..
..
#(Html.X().GridPanel()
.Title("Array Grid")
.ID("propertyGrid")
.Width(600)
.Height(350)
.Store(Html.X().Store()
.Model(Html.X().Model()
.Fields(
new ModelField("PropertyId", ModelFieldType.Int),
new ModelField("PropertyName", ModelFieldType.String),
new ModelField("PropertyShortName", ModelFieldType.String),
new ModelField("PropertyActiveFlag", ModelFieldType.Boolean)
)
).DataSource(properties)
..
..
And i have an action in the controller to add new property. The new property is added successful but I can refresh the PanelGrid (without refresh the whole page). Here is the controller:
[DirectMethod]
public ActionResult AddNewProperty(string propertyName, string propertyCode, bool propertyActive)
{
if (propertyName == "" || propertyCode=="")
{
X.Msg.Show(new MessageBoxConfig
{
Title = "Error",
Message = "The field name or code can not be empty.",
Buttons = MessageBox.Button.OK,
Icon = MessageBox.Icon.ERROR
});
return this.Direct();
}
//if all is ok add new property
var newOne = new StylesPropertyDef
{
PropertyActiveFlag = propertyActive,
PropertyName = propertyName,
PropertyShortName = propertyCode
};
var db = new TaosKnowledgeDataContext(DataUtils.GetConStringLocal());
db.StylesPropertyDefs.InsertOnSubmit(newOne);
db.SubmitChanges();
//reload properties
var properties = db.StylesPropertyDefs.OrderBy(o => o.PropertyId);
var theGrid = X.GetCmp<GridPanel>("propertyGrid");
//now i need refresh or reload the panel grid.
X.GetCmp<Window>("AddNewProperty").Close();
return this.Direct();
//return RedirectToAction("StyleProperties");
}
So, resuming, I need refresh the PanelGrid datasource(or store) from the controller.
Please can you help me?

Please try the following.
Set up an ID for the Store.
.ID("Store1")
Do the following in the controller action.
Store store = X.GetCmp<Store>("Store1");
store.DataSource = db.StylesPropertyDefs.OrderBy(o => o.PropertyId);
store.DataBind();

Ok. I solve.
I deleted the Datasource in Store and I put a proxy reader to Controller.
.Proxy(
Html.X().AjaxProxy()
.Url(Url.Action("Read"))
.Reader(Html.X().JsonReader().Root("data"))
)
In the controller:
public ActionResult Read()
{
var db = new TaosKnowledgeDataContext(DataUtils.GetConStringLocal());
var properties = db.StylesPropertyDefs.OrderBy(o => o.PropertyId);
return this.Store(properties);
}
And when insert new Property:
var store = X.GetCmp<Store>("Store1");
store.Reload();
Thanks anyways.

Related

ASP MVC DevExpress: GridViewSettings in Controller rather than View

I just started working with ASP MVC using DevExpress, I've created views that have GridViews inside, and I had the settings in the partial view.
Short story
I need to have the GridViewSettings object inside a controller rather than the view.
Why? Because the app needs to download an Excel file of the grid and so far this is the only approach I've seen. But the problem is, having the grid settings in the controller doesn't allow me to specify a label inside an unbound column.
Here's the code:
public GridViewSettings MyGridSettings()
{
var settings = new GridViewSettings();
settings.Name = "MyGrid";
settings.CommandColumn.Visible = true;
settings.KeyFieldName = "PERSON_ID";
settings.SettingsPager.Visible = true;
settings.Settings.ShowGroupPanel = true;
settings.Settings.ShowFilterRow = true;
settings.SettingsBehavior.AllowSelectByRowClick = true;
settings.SettingsAdaptivity.AdaptivityMode = GridViewAdaptivityMode.HideDataCellsWindowLimit;
settings.SettingsAdaptivity.AdaptiveColumnPosition = GridViewAdaptiveColumnPosition.Right;
settings.SettingsAdaptivity.AdaptiveDetailColumnCount = 1;
settings.SettingsAdaptivity.AllowOnlyOneAdaptiveDetailExpanded = false;
settings.SettingsAdaptivity.HideDataCellsAtWindowInnerWidth = 0;
settings.Columns.Add("PERSON_ID", "Person ID");
settings.Columns.Add(col =>
{
col.Caption = "Department";
col.SetDataItemTemplateContent(dataTemplate =>
{
String DepartmenID = (String)DataBinder.Eval(dataTemplate.DataItem, "DEPARTMENT_ID");
//if (DepartmenID != null)
//{
// Html.DevExpress().Label(label =>
// {
// label.Text = String.Format("{0}",
// DataBinder.Eval(dataTemplate.DataItem, "DEPARTMENT.NAME"));
// }).Render();
// }
}
}
}
Short Question
How do I access the following code from a Controller?
Html.DevExpress().Label(label =>
{
label.Text = String.Format("{0}",
DataBinder.Eval(dataTemplate.DataItem, "DEPARTMENT.NAME"));
}).Render();
Like I said I need to do this because the Excel File Download. Although, if you have any workarounds where I don't need to do this that'd be great
Using HtmlHelper class described here (read Using HtmlHelper class section), probably this code may solve your issue:
public GridViewSettings MyGridSettings(this HtmlHelper html)
{
var settings = new GridViewSettings();
settings.Name = "MyGrid";
// simplified for brevity
settings.Columns.Add(col =>
{
col.Caption = "Department";
col.SetDataItemTemplateContent(dataTemplate =>
{
String DepartmentID = (String)DataBinder.Eval(dataTemplate.DataItem, "DEPARTMENT_ID");
if (DepartmentID != null)
{
html.DevExpress().Label(label =>
{
label.Text = String.Format("{0}", DataBinder.Eval(dataTemplate.DataItem, "DEPARTMENT.NAME"));
}).Render();
}
}
}
return settings;
}
Unlike views, controller actions doesn't have reference to HtmlHelper by default, hence you need to include HtmlHelper class inside GridViewSettings method to create HTML helper extensions.
In case your DataItemTemplateContent can't be exported by design, try exporting GridView contents to XtraReport using How to convert and then print an GridView extension by using the XtraReport example.
Additional references:
Grid View Exporting - 1
Grid View Exporting - 2

Keeping a page's state

I have already looked at these links for references.
Link 1: ASP.Net MVC and state - how to keep state between requests
Link 2: ASP.NET MVC: Keeping last page state
I have a few pages that a user will be filling out. We will call these pages Page 1. If they get to a field that they need to select from, drop down, but need to create a new item to be included in the drop down, because it will be used again later, they go to a new page, Page 2, to create the item. After create they create the item they are returned to Page 1 to finishing filling out the form. The problem is that the Page 1 is now erased because is a new page load. I would like for this to persist for when they come back so they don't have to refill out fields.
The route I am currently Link2 using a cookie. I don't know how to set the cookie's info before it gets to the next page, or how to pass it to that page before since it is going to a GET method and not a POST.
GET method for Page 1:
public ActionResult Create()
{
var courseTitles = (from title in db.CourseTitles
join type in db.CourseTypes on title.Type equals type.CourseTypeID
select new
{
CourseTitleID = title.CourseTitleID,
Title = title.Title + " - " + type.Type
});
Course course = new Course();
if (Request.Cookies["CourseInfo"] != null) //If it's not null, set the model.
{
HttpCookie cookie = Request.Cookies["CourseInfo"];
course.ClassNumber = Convert.ToInt32(cookie.Values["ClassNumber"]);
course.CourseStartDate = Convert.ToDateTime(cookie.Values["StartDate"]);
course.CourseEndDate = Convert.ToDateTime(cookie.Values["EndDate"]);
ViewBag.CourseList = new SelectList(courseTitles, "CourseTitleID", "Title", cookie.Values["CourseTitle"]);
return View(course);
}
ViewBag.CourseList = new SelectList(courseTitles, "CourseTitleID", "Title");
return View();
}
GET and POST method for Page 2:
public ActionResult NewCourseTitle()
{
ViewBag.Type = new SelectList(db.CourseTypes, "CourseTypeID", "Type");
return View();
}
//
//Post:
[HttpPost]
public ActionResult NewCourseTitle(CourseTitle courseTitle)
{
if (ModelState.IsValid)
{
db.CourseTitles.AddObject(courseTitle);
db.SaveChanges();
return RedirectToAction("Create", "Course");
}
return View();
}
Let me know if you need more code.
You can use TempData to store objects between requests:
public ActionResult Create()
{
var courseTitles = (from title in db.CourseTitles
join type in db.CourseTypes on title.Type equals type.CourseTypeID
select new
{
CourseTitleID = title.CourseTitleID,
Title = title.Title + " - " + type.Type
});
Course course = new Course();
if (TempData["CourseInfo"] != null) //If it's not null, set the model.
{
course = TempData["CourseInfo"] as Course;
ViewBag.CourseList = new SelectList(courseTitles, "CourseTitleID", "Title", course.Title);
return View(course);
}
ViewBag.CourseList = new SelectList(courseTitles, "CourseTitleID", "Title");
return View();
}
In order to store the Course simply use TempData["CourseInfo"] = course
TempData exposes couple of options that define for how long its content is going to be persisted. You can read about it here
You could use some JavaScript to modify the GET request to NewCourseTitle so that it will contain the course data that the user entered.
With jQuery it could look roughly like this:
$(function () {
var newCourseTitleLink = $('#new-course-title-link');
newCourseTitleLink.on("click", function ()
{
document.location.href = newCourseTitleLink.attr('href') + '?' + $('#course-data-form').serialize();
});
});
Then you can create a cookie in your action method NewCourseTitle:
public ActionResult NewCourseTitle(int classNumber, ... /*other form values*/)
{
var cookie = new HttpCookie("CourseInfo");
cookie.Values.Add("ClassNumber", classNumber.ToString());
...
Response.SetCookie(cookie);
ViewBag.Type = new SelectList(db.CourseTypes, "CourseTypeID", "Type");
return View();
}

Dynamic model in partial view doesn't contain a definition for 'FirstOrDefault' after converting to list

I'm trying to create a partial view with a dynamic model but I get the following exception after converting the model to List
Partial View:
#model dynamic
#{
dynamic model;
var obj1ListType = new List<Obj1>().GetType();
var obj2ListType = new List<Obj2>().GetType();
Type unknown = Model.GetType();
if (unknown == obj1ListType)
{
model = new List<Obj1>(Model);
}
else if (unknown == obj2ListType)
{
model = new List<Obj2>(Model);
}
else
{
model = new List<Obj3>(Model);
}
}
The problem is when I try to use
var Obj = Model.FirstOrDefault();
I get the following exception:
'System.Collections.Generic.List' does not contain a definition for 'FirstOrDefault'
Any Ideas? Maybe another way to create a dynamic partial view?
Thanks
I am not sure what you are trying to do here but I modified your code a bit and it works. I had to change few things. Most important change is converting the Model (actually you should have written model that you created right there) to a list and then call FirstOrDefault() on it.
Working Code with some adjustment:
#model dynamic
#{
dynamic model;
var obj1ListType = new List<object>().GetType();
var obj2ListType = new List<object>().GetType();
Type unknown = Model.GetType();
if (unknown == obj1ListType)
{
model = new List<object>(Model);
}
else if (unknown == obj2ListType)
{
model = new List<object>(Model);
}
else
{
model = new List<object>(Model);
}
var data = model as List<object>;
var dataItem = data.FirstOrDefault();
<span>#dataItem</span>
}
Controller:
public ActionResult Partial1()
{
return View(new List<string>(){"Test1", "Test2"});
}
Prints:
Test1

View/Model data isn't refreshing/changing after post/postback, even though I'm using the PRG pattern

Update I have saved my problem a long time ago. The problem was that I was trying to call the view model on the wrong view method! I was calling the base view method (Document), instead of one of it's derived method (like NewDocument, PDFDocument, etc.) Thus it was only giving me the Documents data, which didn't change. I was looking and using the wrong view method all the time... Stephen, when you asked me
"Why do you create derived classes in a method but then return only the base class"
I couldn't answer the question at the time because I didn't even know myself, until I remember that originally, the method wasn't returning the base class. I only changed it so that it can work with the base view method, which was wrong in the first place!
That's what I get for only getting 3-4 hours of sleep in 3 days. Everything works right now. Thanks.
I'm having a hard time trying to figure out why the data in my view isn't changing after I do a post. Originally I was doing it via return View() and it worked, but since it was a partial view, the page didn't look great, so I was reading up and saw that it was better to do it by Post-Redirect-Get pattern (PRG) and to use an id value to retrieve the values instead of sending the entire model via Tempdata. I even used ModelState.Clear() and that didn't even work. When I debugged the code, the model only has the values from when I first called it.
Here's part of my Get controller:
NewDocument Get Controller
[DocumentAuthenticationFilter]
public ActionResult NewDocument(int? id = null)
{
// This doesn't work. The view keeps on showing the data from View(Services.CreateNewDocument()).
if (id != null)
{
return View(Services.GetdocumentViewModelData(DocEnum.Section.NEW_DOC_INDEX, (int)id));
}
// This works fine
return View(Services.CreateNewDocument());
}
And here's the post that calls the redirect:
NewDocument Post controller
[HttpPost]
[ValidateAntiForgeryToken]
[MultipleButton(Name = "action", Argument = "AddDocuments")]
//[OutputCache(Duration = 30, VaryByParam = "*")]
public ActionResult AddDocumentViewModel(FormCollection frm, DocumentViewModel dvm)
{
try
{
if (ModelState.IsValid)
{
int? DocID = Services.AddingNewDocument(dvm);
// See, I even tried to clear it.
ModelState.Clear();
return base.RedirectToAction("NewDocument", new { id = DocID });
}
else
{
// Display errors in the modal
}
return base.RedirectToAction("NewDocument");
}
And here's the old way I did it:
NewDocument Post controller
[HttpPost]
[ValidateAntiForgeryToken]
[MultipleButton(Name = "action", Argument = "AddDocuments")]
//[OutputCache(Duration = 30, VaryByParam = "*")]
public ActionResult AddDocumentViewModel(FormCollection frm, DocumentViewModel dvm)
{
try
{
if (ModelState.IsValid)
{
Services.AddingNewDocument(ref dvm);
dvm.NewRecordMode = DocEnum.Action.UPDATE;
// It worked, but only the partial view showed, and not the entire view.
return PartialView("_NewDocument", dvm);
}
else
{
// Display errors in the model
}
return base.RedirectToAction("NewDocument");
}
Could it be because I'm using a custom model binding?
My Custom Model Binding
public class BaseClassModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var modelType = bindingContext.ModelType;
var modelTypeValue = controllerContext.Controller.ValueProvider.GetValue("ViewModel");
if (modelTypeValue == null)
throw new Exception("View does not contain the needed derived model type name");
var modelTypeName = modelTypeValue.AttemptedValue;
var type = modelType.Assembly.GetTypes().SingleOrDefault(x => x.IsSubclassOf(modelType) && x.Name == modelTypeName);
if (type == null)
{
throw new Exception(String.Format("Derived model type {0} not found", modelTypeName));
}
var instance = bindingContext.Model ?? base.CreateModel(controllerContext, bindingContext, type);
bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => instance, type);
return base.BindModel(controllerContext, bindingContext);
}
}
EDIT: And here's the GetDocumentViewModelData code:
GetDocumentFromViewModelData
public static DocumentViewModel GetDocumentViewModelData(DocEnum.Section docType, int id)
{
switch (docType)
{
case DocEnum.Section.NEW_DOCUMENT_INDEX:
// NewDocumentTypeViewModel is a child to DocumentTypeViewModel
DocumentTypeViewModel nd = NewDocumentService.GetViewModelByID(id);
return nd;
case DocEnum.Section.PDF_DOCUMENT:
DocumentTypeViewModel pdfvm = PDFDocumentService.GetViewModelByID(id);
return pdfvm;
case DocEnum.Section.XLS_DOCUMENT:
DocumentTypeViewModel xlsvm = XLSDocumentService.GetViewModelByID(id);
return xlsvm;
}
return null;
}
Edit: Also adding the GetViewModelByID function
GetViewModelByID
public static DocumentTypeViewModel GetViewModelByID(int id)
{
docEntities db = new docEntities();
NewDocumentTypeViewModel vm = new NewDocumentTypeViewModel();
// Calls a stored procedure called Select_Documents_ByID(id) to get the note entry
// that was submitted.
List<Select_Documents_ByID_Result> prevNotes = db.Select_Documents_ByID(id).ToList();
StringBuilder sNotes = new StringBuilder();
foreach (var note in prevNotes)
{
sNotes.AppendFormat("{0} - {1}: {2}\n\n", note.CreatedDate.ToString("yyyy-MM-dd HH:mm"), note.username, note.Entry);
}
vm.PreviousNotes = sNotes.ToString();
return vm;
}
Edit: I did a direct creation of the view model inside the Get controller, and it's the same result. when i debugged the view itself, the values from the new view model don't show up. Instead, the values from the initial view model, View(Services.CreateNewDocument()), shows.
[DocumentAuthenticationFilter]
public ActionResult NewDocument(int? id = null)
{
// Right here I created the view model to test thing, but I'm getting the same results. Nothing has changed.
if (id != null)
{
var d = new NewDocumentTypeViewModel(1, "Help!");
// This property is from the base class, DocumentTypeViewModel
d.DocumentTitle = "Testing!";
return View(d);
// Inside the view itself, none of the values in the view model, including the one
// belonging to the base class. It still shows the initial values.
}
// This works fine
// Or maybe not...
return View(Services.CreateNewDocument());
}
Edit: I wanted to see if it was also doing the same thing for the initial call to the view return View(Services.CreateNewDocument()), and decided to change the value for documentTitle in the base class from New Document to a randomly-generated number, after the object has been created.
Here's the code for DocumentTypeViewModel's default constructor:
public DocumentTypeViewModel()
{
DocumentTitle = "New Document";
NewRecordMode = DocEnum.Action.ADD;
DocumentID = 0;
}
And here's the Services.CreateNewDocument() code where I change the DocumentTitle after the View Model has been created.
public DocumentTypeViewModel CreateNewDocument()
{
DocumentTypeViewModel dtvm = new DocumentTypeViewModel();
Random r = new Random();
dtvm.DocumentTitle = r.Next(5, Int32.MaxValue).ToString();
return dtvm;
}
Now in the View, when I call DocumentTitle:
<div class="label-text-group">
#Html.LabelFor(model => model.DocumentTitle)
#Html.EditorFor(model => model.DocumentTitle)
</div>
You would expect to see a randomly-generated number every time the View gets called. Nope, what you would see is "New Document". Weird.
It's seems that Services.GetDocumentViewModelData() is not exactly working correctly. It only carries the values created by the base class' constructor when a view is created, not any values that have been added or changed within GetDocumentViewModelData() itself. Why is that? What's going on? Please help anybody!
I have solved it. Look at the Update section on top. Thanks Stephen.

MVC: Edit and create in same [HTTPOST] action method

I am using MVC3-Viewmodel model first on my project.
When a user enters a value in my DDL and TextArea and then click on my form button it will basicly execute a ajax url.post to my POST action, right now my Post Action method creates and saves it. But what I want is some type of check, example:
step 1: If SelectQuestion has any answer
step 2: If answer exist do an update
step 3: if answer do not exist create a new and save it.
This is how my controller looks like now:
[HttpPost]
public JsonResult AnswerForm(int id, SelectedQuestionViewModel model)
{
bool result = false;
var goalCardQuestionAnswer = new GoalCardQuestionAnswer(); // Creates an instance of the entity that I want to fill with data
SelectedQuestion SelectedQ = answerNKIRepository.GetSelectedQuestionByID(model.QuestionID); // Retrieve SelectedQuestion from my repository with my QuestionID.
goalCardQuestionAnswer.SelectedQuestion = SelectedQ; // Filling my entity with SelectedQ
goalCardQuestionAnswer.SelectedQuestion.Id = model.QuestionID; // filling my foreign key with the QuestionID
goalCardQuestionAnswer.Comment = model.Comment; // Filling my entity attribute with data
goalCardQuestionAnswer.Grade = model.Grade; // Filling my entity attribute with data
answerNKIRepository.SaveQuestionAnswer(goalCardQuestionAnswer); // adding my object
answerNKIRepository.Save(); // saving
result = true;
return Json(result);
}
Comment and Grade are nullable aswell.
The entitys are associated like
[Question](1)------(*)[SelectedQuestion](1)-----(0..1)[GoalCardQuestionAnswer]
Any kind of help is appreciated.
Thanks in advance!
I achieved my question and the answer is following:
[HttpPost]
public JsonResult AnswerForm(int id, SelectedQuestionViewModel model)
{
SelectedQuestion SelectedQ = answerNKIRepository.GetSelectedQuestionByID(model.QuestionID);
if (SelectedQ.GoalCardQuestionAnswer == null)
{
var goalCardQuestionAnswer = new GoalCardQuestionAnswer();
goalCardQuestionAnswer.SelectedQuestion = SelectedQ;
goalCardQuestionAnswer.SelectedQuestion.Id = model.QuestionID;
goalCardQuestionAnswer.Comment = model.Comment;
goalCardQuestionAnswer.Grade = model.Grade;
this.answerNKIRepository.SaveQuestionAnswer(goalCardQuestionAnswer);
this.answerNKIRepository.Save();
const bool Result = true;
return this.Json(Result);
}
else
{
if (SelectedQ.GoalCardQuestionAnswer != null)
{
SelectedQ.GoalCardQuestionAnswer.Comment = model.Comment;
}
if (SelectedQ.GoalCardQuestionAnswer != null)
{
SelectedQ.GoalCardQuestionAnswer.Grade = model.Grade;
}
const bool Result = false;
return this.Json(Result);
}
}

Resources