ASP.net
Awesome
Learn
Forum
Buy
Demos
Sign In
☾
☀
Switch to
Dark
Light
Mode
this site works best with javascript enabled
Ask Question
Model with type long Id
Title:
B
I
{code}
?
I have a Model with type long Id ... I am having trouble editing and deleting registration Examples IDs causing editing and exclusion problems; 20150819023814215 20150819023818448 20150819023822730 I made this extension method to have a unique ID of type long public static long ToGuidLong (this object o) { return (DateTime.Now.ToString("yyyyMMddHHmmssfff").ToInt64() + new Random().Next(0, 1000)); } my view <h2>Cadastro de Pessoa</h2> @Html.InitCrudPopupsForGrid("PessoaGrid", "Pessoa") <div class="bar"> <button type="button" onclick="awe.open('createPessoaGrid')" class="awe-btn mbtn">Create</button> </div> @(Html.Awe().Grid("PessoaGrid") .Mod(o => o.PageInfo().ColumnsSelector()) .Url(Url.Action("GridGetItems", "Pessoa")) .Height(350) .Columns( new Column { Name = "Id", Width = 250 },//Id type long new Column { Name = "NomeRazao" }.Mod(o => o.Nohide()), new Column { ClientFormat = GridUtils.EditFormatForGrid("PessoaGrid"), Width = 50 }.Mod(o => o.Nohide()), new Column { ClientFormat = GridUtils.DeleteFormatForGrid("PessoaGrid"), Width = 50 }.Mod(o => o.Nohide()))) my controller method public ActionResult GridGetItems(GridParams g, string search) { search = (search ?? "").ToLower(); var items = crud.BuscaTodos().Where(o => o.NomeRazao.ToLower().Contains(search)).AsQueryable(); var li = items.ToList(); return Json(new GridModelBuilder<Pessoa>(items, g) { Key = "Id", // needed for api select, update, tree, nesting, EF GetItem = () => crud.BuscarPorId(Convert.ToInt64(g.Key)), // called by the grid.api.update ( edit popupform success js func ) Map = MapToGridModel }.Build()); }
Save Changes
Cancel
Leonel Jaime
asked at 19 Aug 2015
I'm sorry I can't understand you description of the problem, please give more details, or reproduce the problem in a mini project and post it here, please read this http://aspnetawesome.com/learn/mvc/CommonProblems#Isolate-the-problem
at 19 Aug 2015
Omu
Answers
B
I
{code}
?
This is how I solved it: run the solution noticed dinners Id is not unique when clicking Edit on Grid row saw js error in the browser console (Ctrl+Shift+I in chrome to see console) changed action Edit(int id) to Edit(long id) ChefLookupController changed action GetItem(int? v) to GetItem(long? v) try to Edit and save I get in the browser console "Value was either too large or too small for an Int32." on line: GetItem = () => Db.Get<Dinner>(Convert.ToInt32(g.Key)) changed to Convert.ToInt64 now the edit is successful except all rows that also show the same Id get replaced with the updated one did some debugging, noticed that on the server ids are different but when they go to the client I see the model in the browser console (Network tab), the Ids are not unique anymore found this http://stackoverflow.com/questions/25839498/long-parsing-incorrectly-json-response-asp-net-mvc turns out browser can't handle longs that big, we need to use strings instead in DinnersGridCrudController changed MapToGridModel from new { Id, to Id = o.Id.ToString(CultureInfo.InvariantCulture), Edit and Delete post actions: return Json(new { Id = dinner.Id.ToString(CultureInfo.InvariantCulture) }); in GridUtils surrounded the Id value with apostrophe awe.open('{0}', {{ params:{{ {1}: '.{1}' }} }}) --- and one more thing, about your extension, try to run this unit test: public static long ToGuidLong() { return Convert.ToInt64(DateTime.Now.ToString("yyyyMMddHHmmssfff")) + new Random().Next(0, 1000); } [Test] public void TestJson() { var s1 = ToGuidLong(); var s2 = ToGuidLong(); var s3 = ToGuidLong(); var s4 = ToGuidLong(); var s5 = ToGuidLong(); Console.WriteLine(s1); Console.WriteLine(s2); Console.WriteLine(s3); Console.WriteLine(s4); Console.WriteLine(s5); } you'll see that s2, s3, s4, s5 are the same, so *you should not use this extension to create unique Ids* , Random uses a seed which by default is the current time tick, and since this code is fast it runs in the same tick so you get the same value and you can use Guid, just make sure to replace all int related stuff to string or Guid
Save Changes
Cancel
Omu
answered at 19 Aug 2015
please
Sign In
to leave an answer
By accessing this site, you agree to store cookies on your device and disclose information in accordance with our
cookie policy
and
privacy policy
.
OK