JQuery to update textbox depending on DDL selection - c#

I have a serial number generation method on MVC 4
When i select a Product ID from the DDL and press create it, it generates a serial number no problem to the Index page
However i would like to have the Serial number presented to the user during the creation process, so as soon as they select a value from the DDL, the serial number will be generated and displayed in a read only box.
Below is my snippet that does the serial generator
//
// POST: /Item/Create
[HttpPost]
public ActionResult Create(Item item)
{
if (ModelState.IsValid)
{
string SerialNumber = string.Format("SN/{0}/{1}", DateTime.Now.Year, item.ProductID);
item.SerialNumber = SerialNumber;
db.Items.Add(item);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ProductID = new SelectList(db.Products, "ProductID", "ProductID", item.ProductID);
return View(item);
}
I have very little experience with jquery so it would be great to get pointers or help on how to accomplish this idea. If there is even a better way than jquery please let me know :)
Any info requests i will be happy to supply
Thanks

this will give you can exact idea binaryintellect.net/articles/… – #Frebin Francis (credit goes to Frebin)

Related

Set page count on dynamic pages & maintain page visit count in database

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"}))

How To Find A Guid within an If Statement

Here is the problem. User can come to set and enter in text in an upper and lower text box. After they have done so that text will then be stored in a datatable which will hold a Guid Id, a Title string, and a Body string.
How do I arrange it so that a if the page reloads (meaning the data gets sent to the data base) that if the user changes come text and hits submit again that the table will change accordingly and not make a new row.
I am thinking I will need an if statement to pull this off but I am not sure what to put within the statement. There is the code I have thus far in a .net mvc controller
[HttpPost]
public ActionResult ActName(ModelNameModel item)
{
if( ModelState.IsValid)
{
using (DB.DatabaseName db = new DB.DatabaseName())
{
DB.Model newRecord = new DB.Model();
newRecord.Title = item.Title;
newRecord.Body = item.Body;
newRecord.Id = Guid.NewGuid();
db.Models.Add(newRecord);
db.SaveChanges();
}
}
return View(item);
}
What other if statement can I use to verify the Guid Matches? Thank you!

Values added 2-7 times to database

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");
}

MVC reCapcha Only Returns False

I am using:
http://mvcrecaptcha.codeplex.com/
My problem is very simple!
bool captchaValid
always returns false, no matter what I do.
Here is my code:
[CaptchaValidator]
[HttpPost]
public ActionResult ViewWidget(int id, TagwallViewModel model, bool captchaValid)
{
model.TagwallCollection = new TagWallCollection() { Id = id };
if (!captchaValid)
{
ModelState.AddModelError("_FORM", "You did not type the verification word correctly. Please try again.");
}
else
It shows no errors..
Things i have done differently, but i think have no influence:
The cs files downloaded from codeplex is not in the same folders.
I registered on https://www.google.com/recaptcha/admin/create to get my two keys with a online domain, but i'm testing it on localhost.
That was my problem, sorry for troubleing you! Having a bad code day.
I am using Razor.

C# MVC NerdDinner authorization helper method problem

I'm currently going through the ASP.NET MVC NerdDinner tutorial and am having a problem with a particular helper method related to user authorization. The idea is that only users who "own" a particular dinner should be able to edit or delete it (based on the Dinner object's HostedBy property).
I have the following method in my Dinner object:
public partial class Dinner {
public bool IsHostedBy(string userName) {
return HostedBy.Equals(userName, StringComparison.InvariantCultureIgnoreCase);
}
// other stuff removed for brevity
}
and in my View I'm trying to show/hide links based on whether the logged in user is the dinner's host:
<% if (Model.IsHostedBy(Context.User.Identity.Name)) { %>
<%= Html.ActionLink("Edit Dinner", "Edit", new { id = Model.DinnerID })%>
|
<%= Html.ActionLink("Delete Dinner", "Delete", new { id = Model.DinnerID })%>
<% } %>
The problem is that IsHostedBy() never returns true. I've written User.Identity.Name and Dinner.HostedBy to the screen to verify they're the same, but the method still returns false. I'm uncertain how to track down the problem.
I'm new to both C# and ASP.NET MVC, so it's very likely I'm missing something easy. Any help is appreciated and I'd be happy to post more information if it's needed.
While I'm at it I may as well write the Answer.
Check for errent spaces in the two strings.
I'm guessing that HostedBy and userName aren't actually the same string!
Some debugging ideas:
1st) Try forcing it to always return true:
public bool IsHostedBy(string userName) {
return true;
}
If this lets you return true back into the view, at least you can know that the code you're writing in the IsHostedBy method is being executed.
2nd) Add a console-out to see for yourself if the two strings are indeed equal:
public bool IsHostedBy(string userName) {
Console.WriteLine("userName: {0} / HostedBy: {1}", userName, HostedBy);
return true;
}
This will help you inspect the values of these items. Or you could just set a breakpoint at the return statement and see what they are as well.

Categories

Resources