Mvc use string on multiple page - javascript

I'm making a patient web app. When user add new patient, I want to keep this patient information(name, surname, phone etc.) I created a static class for patient info values. I set datas on this class. How can I get this info on another views?
This is info class: https://imgur.com/a/LnncS3S
I tried access on view like that:
#{Dentilog.Models.patientinfo.patient_id}
I getting data on views like that (I already set data values on form input)
https://imgur.com/a/h4JprXO
and in controller: https://imgur.com/a/225dBe6
I dont have any problem for getting data. How can use that data on another page?

Related

Trying to load data into a new view

I have an issue attempting to create a "clone" of data on a page. The summary is that I have a create customer jobs screen and a separate manage customer jobs screen. These both work fine. I have a need to "clone" already existing customer job data to create a new similar set of jobs. So in my management screen, I have a clone button. I want this to head off to my "create" page with the data pre-populated with the original data, minus one or two pertinent identifying pieces of information, e.g. Job Name.
My issue is I cannot seem to get the post to redirect off to the Create page with my new data in the view model. I can't have a submit action as this saves the data; the data I'm cloning won't be persisted between the manage and create screens.
Here is where I'm at:
In my CSHTML:
<button type="button" name="btnClone" value="btnClone" id="btnClone" formaction="CloneJob" class="btn btn-primary" style="width: 150px;">Clone</button>
This fires off a click event to grab the current jobId which then fires off this function:
function CloneJob(jobId) {
$.post('CloneJob', { JobId: jobId }, function (data) {
window.document(data);
});
}
In my controller, I have this:
[HttpPost]
public ActionResult CloneJob(Guid jobId) {
// logic which wipes off the old data job from the view model
// eg names, ids, etc. but leaves the actual job details intact
return View("CreateJob", manageJobViewModel);
From this, I can see the HTML being returned in the IE/Chrome debug network section, but it won't go off to my CreateJob view populated with the data.
What am I missing?
First, if you're going to replace the entire tab/window contents, don't use AJAX. There's no point, and it's just something extra to maintain. Utilize a standard link, instead. Second, a POST should only be made when something is changing. This here should just be a simple GET request.
Create a link to the "create" URL and pass an id for the job that's being cloned. In the create action, then, you can look up that job from the database, based on that id, populate your view model with the data, and return it with the view.
If you want the appearance of a button, you can just style the link to look like a button, but you should use a standard old <a> tag, because that's the semantic meaning here.
I have managed to get further in that it now "works". My controller is unchanged, but my javascript is now:
$.when($.get('CloneJob', {jobId: jobId}))
.then(function (response)
{ $("body").html(response); });
I'm aware that this is still ajax, but in the face of an alternative that is more technically acceptable, this does work. Would be happy to take any advaces for a more technically neater approach as I understand that this is a bit of a misuse of ajax.
I have managed to get this cracked.
My cshtml now contains this code:
#Html.ActionLink("Clone Job", "CloneJob", null, new { #class = "btn btn-default", #style = "width: 150px" })
My controller just grabs the current viewmodel from the session and uses it to clone the job. Then, the controller now simply returns the view with the new model I set up within the method.
return View("CreateJob", clonedJobViewModel);
It's simple now when I look at it, but it's the getting there part.

How to save multiple records in the saveRecord function through nlapi methods?

I have customized the default promotion form in which if a user clicks on a particular promotion the form will show dynamic elements using jquery ( as we have not created the fields or lists/records for this requirement).
so based on the data we may need to create multiple records while submitting the form. I have created the saveRecord() function in js file which is already mapped with the form. In that am trying to create promotionRecord dynamically. But cant save the record I am getting "the items you requested in the record have been deleted since you retrieved the form" error. What could be the problem and is it possible to save multiple record in single form submit?
departments = Object.keys(samplePromotions);
$.each(departments, function(key,value){
console.log(samplePromotions[value]);
promotion = nlapiCreateRecord('customrecord_promotion');
console.log(promotion);
//required
promotion.setFieldValue('name','jAVASCRIPT pROMOTION cREATION_'+value);
promotion.setFieldValue('custrecord_px_promotion_id','js_prom_creation_'+value);
id = (Math.round(new Date().getTime()/1000));
console.log(id);
promotion.setFieldValue('id',id);
var id = nlapiSubmitRecord(promotion);
Not sure if this is the problem in your code, but to answer your question you can only submit one record with the API.
I would just create an array and loop through the length of it and call nlapiCreateRecord and the nlapiSubmitRecord for each one.
-- Edit
Not sure about this, but this looks weird to me to. You are setting the id using setFieldValue method, which I believe is the external Id. Then you are setting setting id again with the nlapiSubmitRecord call. This returns the internal Id. Maybe that is causing issues as well?
Must be this line
promotion.setFieldValue('id',id);
The id field on the record object contains the internal id of the record. This is generated by NetSuite when you create a record.

Auto refresh label in MVC View

I have an auto refresh method (something like below) in my controller. In which I will update data in ViewBag, which I use in label of view. But I'm unable to refresh my label automatically. Please help me.
var waitHandle = new AutoResetEvent(false);
ThreadPool.RegisterWaitForSingleObject(waitHandle,(state, timeout) =>
{
// my viewbag
viewbag.time = DateTime.Now.TimeOfDay;
viewbag=date
}, null, TimeSpan.FromSeconds(5), false);
MVC doesn't work like WebForms (where reassigning a value populates to the UI). You'll need to write a separate action (or WebAPI method) and use AJAX/JavaScript to pull updates. (Alternatively you could use SignalR, but that may be overkill).
Also, if this is a recurring event, you may want to look into a library like Quartz.NET to perform the action. Then, post updates to a shared resource (looks like you're storing timestamp of last execution). From there, use setInterval/AJAX on the client to retrieve and display that value.

Jaydata will object still exist after table drop and rebuild

Lets say I have two tables task and person.
//Create related data
todoDB.Todos.add({
Task: "Your task",
Person: new Person({Name: 'Peter'});
});
todoDB.saveChanges();
I'm trying to implement a way of keeping all the client and server data in sync.
Lets say I changed the schema of person and need to rebuild that table --> I drop person and rebuild it with the new schema.... re-populate it with data including a person with name=Peter. Will "Your task"'s person attribute still link to that same person, or will that attribute need to be rebuilt. I plan use an id attribute in reality and not name, and Id will be a key value.
If your sync happens only one-way direction, you can use the IDs from the server, but if you create entities on the client and you want to sync back to server, the GUID reference types are the way to go to have the same identifiers on the client and the server without conflicts.

AngularJS and embedded framework html

I'm developing a user preferences page and I've decided to use AngularJS to control the state of the data.
What I would like to achieve is a page that shows the current user's settings and allows a user to edit them, cancel their changes, or save & submit changes if they so desire. I've binded my labels and my input controls to the model so that when a user modifies their email address, it is reflected across correctly.
I'm basing my work off this example #3 by the way: http://code.angularjs.org/1.0.0rc12/docs-1.0.0rc12/guide/forms
My question is related to the default values for the page. Since the model is defined as an object in Javascript. In the link above, default values can be set over here in $scope.master= {}; which works fine except that I'm using a framework to generate a static page and not retrieving an json object from my server. My pages are all written in embedded scala, so I access the values serverside. What is the best way to take the info retrieved serverside and turn into an object client side javascript can access?
You have a few choices. The way I would do it would be to push the initial state (through a WebSocket or something) to Angular as JSON, or have Angular pull it from the server through an HTTP call.
You could also perhaps render the page with ng-init tags to set the initial state, but this way seems weird to me.
<div ng-controller="FormController" ng-init="formData = {}">
<form>
<input ng-model="formData.input1" ng-init="formData.input1 = 'initialValue'>
</form>
</div>
I am using ASP.NET and Dapper-Dot-Net. I use the following techniques to get my data on to the page and then into Angular scope. It should be pretty transferable to your environment.
Define a DataTransferObject class in the page on the server side and a public string called DtoJson to hold the serialized version of your class instance.
private class DataTransferObject{
public User User;
public List<Project> Projects
}
public string DtoJson;
var dto=new DataTransferObject();
var userName="however you get a username";
dto.User=User.Get(userName); // I already have a User class
var sql="select * from projects where user_name=:UserName";
var p=new DynamicParameters();
p.Add(":UserId", dto.User.ID);
dto.Projects=Project.Query<Project>(sql, p); // returns a list of Project
// I use json.net to convert the Dto to json and expose it as a public variable
var DtoJson=Json.Convert(dto); // approximate syntax but you get the idea
On the client side I put a script tag at the head of the page and inject the DtoJson into it to get it into the global scope
<script>var dto=<%=DtoJson%>;</script>
At the bottom of my page I have a script src="pageName.js" which is my Angular controller file. Inside the controller I say
$scope.dto=dto;
console.log($scope.dto);
Now the json that was created on the server side is in my controller's scope and I can data bind to {{dto.User.FIRST_NAME}} and {{dto.Projects[0].ID}} ng-repeat="for p in dto.Projects" etc
I hope that that makes sense.
Peter

Categories

Resources