I just want to know the logic of this. I am not posting any code because I don't know what is the logic behind this so please pardon. I have a page call dynamicPage. This page is connected with database & everytime details of particular things gets fetched according to users selected. Now I want to track How many time particular thing is visited.
e.g.
Hospital1
Hospital2
Hospital3
If user clicks on Hospital2 then it's count get increased by one & so on..
I made this site http://www.brandstik.in/Music here many products are listed. Now I want to see how many times particular products is viewed.
You need something between clicking on the product link and loading the detail page. There are lots of ways to do that. One of the easiest ones is to have a method between you clicked on the link and loading the detail page. So the simplest solution that I can suggest is to have an action (if it is MVC) or a simple method on detail page, increase the count and then redirect to the original one. So lets say your code is in MVC and you have a method like this on dynamic page:
#Html.ActionLink("#item.productName", "Index", "Products", new {id = "#item.id"}))
and you have this code in your products controller:
public class ProductsController:Controller
{
Public ActionResult Index(int Id)
{
...some code to load and return the productDetails
}
}
Then you need to add a method to add to the count and then redirects to the original method. so your controller will be like this:
public class ProductsController:Controller
{
Public ActionResult Index(int Id)
{
//some code to load and return the productDetails
}
public ActionResult IncreaseProductCount(int Id)
{
//increase the count
return RedirectToAction("Index",new{Id=Id});
}
}
And then on the dynamic page view change your code to call the new method instead:
#Html.ActionLink("#item.productName", "IncreaseProductCount", "Products", new {id = "#item.id"}))
Related
I'm fairly new to mvc, and have started learning asp.net mvc 5 and django
I want to create an application where the user can create a new view at runtime. So lets say I create a feature in the web app for a user to add a new page where they can fill out a form, say the title maybe text, or fields they want to display on the view, and when the user saves it that info gets saved to the db and creates a new view.
My questions are:
can you create dynamic views at runtime?
how do you create the proper url to route to that new page?
if the 1st two are possible can you use a model or viewModel to then display the content from the db for that page?
Any advice on if this can be done would be appreciated. Thanks
I think you need to make a page that saves the user configuration in db as per user demand.
From my end, I suggest the following approach to do it.
Whatever you gets the data from database which are returns like as below snap.
Make one Action in controller and assign those data in one datatable/list in action.
public ActionResult LoadContent()
{
dynamic expando = new ExpandoObject();
var model = expando as IDictionary<string, object>;
/*Let say user insert the detail of employee registration form. Make the
database call and get the distinct detail of particular inserted form by Id
or whatever. As an example below datatable contains the data that you fetch
during database call.*/
DataTable objListResult =
HeaderViewActionHelper.GetFinalResultToRenderInGenericList(id);
if (objListResult != null && objListResult.Rows.Count > 0)
{
foreach (DataRow row in objListResult.Rows)
{
model.Add(row["DisplayName"].ToString(),
row["DisplayNameValue"].ToString());
/*If you want to handle the datatype of each field than you can bifurcation
the type here using If..else or switch..case. For that you need to return
another column in your result from database i.e. DataType coloumn. Add in
your model as, model.Add(row["DisplayName"].ToString(),
row["DisplayNameValue"].ToString(), row["DataType"].ToString());*/
}
}
/* return the model in view. */
return View(model);
}
In the view you can go through the loop over modal and render the detail.
#model dynamic
#using (Html.BeginForm("SubmitActionName", "ControllerName")
{
<div class="table-responsive">
<table>
#if (Model != null)
{
foreach (var row in Model)
{
}
}
</table>
</div>
}
For more detail, Please go through this link.
So I'm trying to add a new row of information into the database table I'm using and currently it sometimes adds from 2-5-7 rows of the same information into to the database but with different Id's.
I have tried to debug through the process and I don't understand why the ActionResult runs several times and adds the same information up to 7(!) times.
When I click the add button it starts to load the the next page and the values are added to the database. The problem is that about 5-6 seconds later after clicking the add button it goes back to the controller again and do the same process again witch result in a random number of 2-5-7 rows added with the same values but different id's.
I am using a simple View with editor-fields and a dropdown list to select countries and it is posted to the a ActionResult that adds the values into the database.
The ActionResult look like this:
[HttpPost]
public ActionResult AddNewsdesk(AddNewsdeskViewModel addNewsdesk)
{
if (ModelState.IsValid)
{
var newsdesk = Mapper.Map<AddNewsdeskViewModel, Newsdesk>(addNewsdesk);
newsdesk.Country = _databaseContext.Countries.Single(c => c.Name.Equals(addNewsdesk.Country));
newsdesk.Id = new Guid();
_databaseContext.Add(newsdesk);
_databaseContext.SaveChanges();
return RedirectToAction("Index", "Newsdesk");
}
return RedirectToAction("Index", "Home");
}
I have two different pages, from which a user can click on a 'details' link and go to the details page.
On the details page, I have a 'back' button, which leads the user to the originating page, being one of the two original pages of course.
There is also one extra issue: in one of the return links, I must specify an extra anonymous object.
my view code right now is:
#{
MvcHtmlString backLink = null;
if (Model.ReturnPage == MatchResultReturnPage.Search)
{
backLink = Html.ActionLink("GoBack", "Search", new {search = true});
}
else
{
backLink = Html.ActionLink("GoBack", "Dashboard");
}
}
In the controller I now look in the url.referrer if it contains 'dashboard', then I set the Model.ReturnPage to 'Dashboard'.
Is there a cleaner way of doing this?
Put the ReturnLink as a property on your model and set it inside the controller, which will alleviate the need for you to put that logic in the view.
There are certainly cleaner ways, but as your code is currently, it is very easy to understand what you are trying to do.
I would say keep it as is and simply put a #region wrapper around it and hide it when you don't need to work with it:
#region get referrer page
MvcHtmlString backLink = null;
if (Model.ReturnPage == MatchResultReturnPage.Search)
{
backLink = Html.ActionLink("GoBack", "Search", new {search = true});
}
else
{
backLink = Html.ActionLink("GoBack", "Dashboard");
}
#region
The only thing I would suggest is to have this check in the Controller, rather than the view and simply putting the result of your check either in model property, or in the ViewBag.
To gain access to Helpers in your controller, do the following:
var URL = new UrlHelper(this.Request.RequestContext).Action("MyAction", "MyController", new { id = 123 });
you should probably implement the Back button entirely in JavaScript.
using the history object
<a href=”javascript:history.back()”> [Back]</a>
I'm wanting to capture the old values within a model so I can compare with the new values after submission, and create audit logs of changes a user makes.
My guess is doing it with hidden input boxes with duplicated old value properties would be one way. But wondering if there are any other good alternatives?
Thanks
In the save method, just go and get the original object from the database before saving the changes, then you have your old and new values to compare against? :)
This sounds like standard auditing. You should not worry about what has changed just capture EVERYTHING and who made the change. Unless there is some sort of real time reporting that needs to be done.
Possible auditing implementations:
CQRS, in a nutshell it tracks every change to a given object. The downside is it's an architecture that is more involved to implement.
The Rolling ledger. Each insert is a new row in the database. The most current row is used for display purposes, but with each update, a new row is inserted into the database.
Yet another approach is to save it off into an audit table.
All get the job done.
You could also store the original model in the view bag and do something like this...
// In the controller
public ActionResult DoStuff()
{
// get your model
ViewBag.OriginalModel = YourModel;
return View(YourModel);
}
// In the View
<input type="hidden" name="originalModel" value="#Html.Raw(Json.Encode(ViewBag.OriginalModel));" />
// In the controller's post...
[HttpPost]
public ActionResult DoStuff(YourModel yourModel, string originalModel)
{
// yourModel will be the posted data.
JavaScriptSerializer JSS = new JavaScriptSerializer();
YourModel origModel = JSS.Deserialize<YourModel>(originalModel);
}
I didn't get a chance to test this, just a theory :)
Exactly what mattytommo says is the preferred method all around
Instantiate new view model for creating a new entity
public ActionResult Edit(int id) {
var entity = new Entity(id); // have a constructor in your entity that will populate itself and return the instance of what is in the db
// map entity to ViewModel using whatever means you use
var model = new YourViewModel();
return View(model);
}
Post changes back
[HttpPost]
public ActionResult Edit(YourViewModel model) {
if (ModelState.IsValid) {
var entity = new YourEntity(model.ID); // re-get from db
// make your comparison here
if(model.LastUserID != entity.LastUserID // do whatever
... etc...
}
return View(model);
}
I am working through sample MVC Nerdinner tutorial and using it on the AdventureWorks database. I have created an Edit action in the CategoryController to edit Product Category in AdventureWorks. The only updateable field in this table is the Name (the other fields - ID, RowGUID and UpdateDate are autogenerated). So my edit form View has only 1 field for the Name (of Product Category). My "Save" action for the edit is below: -
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection collection){
ProductCategory p = awRepository.GetProductCategory(id);
try
{
//UpdateModel(p);
p.Name = Request.Form["Name"];
awRepository.Save();
return RedirectToAction("Details", new { id = p.ProductCategoryID });
}
catch
{
foreach (var err in p.GetRuleViolations())
{
ModelState.AddModelError(err.PropertyName, err.ErrorMessage);
}
return View(p);
}
}
If I use the code as above, everything works as long as the Name I enter is valid (thus there is no exception). If I introduce an error (which is raised by GetRuleViolations if the Name is blank or for testing purposes is a particular "Test" string) I get a NullReferenceException (Object reference not set to an instance of an object) on this line in the View (Category/Edit.aspx) when the Edit View is redrawn (to show the user the error and allow him to correct)
<%= Html.TextBox("Name") %>
If I update my ProductCategory using UpdateModel(p) instead of using the Request.Form variable, everything works fine; Valid data is saved and invalid data redraws the view showing the error message.
My question is: what is the difference between UpdateModel and manual updating my variable by reading the values from Request.Form collection? The Nerdinner tutorial seems to suggest that both are equivalent. So I am surprised that one works smoothly and the other raises an exception.
Sounds like this:
http://forums.asp.net/p/1396019/3006051.aspx
So, for every error you add with
ModelState.AddModelError() and call
the View again, MVC Framework will try
to find an AttemptedValue for every
error it finds. Because you didn't add
them, MVC will throw an exception.
Normally you don't need to add these
values: AttemptedValues are
automaticaly populated when you use
DefaultBinding (by calling
UpdateModel() or by passing the object
to bind as an Action Method paramter:
public ActionResult
Create(FormCollection Form,
YourObjectType yourObject).
Looks like the following is done automatically by UpdateModel, but not done manually by yourself?
if (Form["Name"].Trim().Length == 0)
{
ModelState.AddModelError("Name", "Name is required");
//You missed off SetModelValue?
ModelState.SetModelValue("Name", Form.ToValueProvider()["Name"]);
}