Reputation: 971
I am saving "Instruction" in CreateInstruction view. This view has partial view "CreateInstnAttribute" which saves "Attributes" related to this Instruction. It requires the InstnID as foreign key. The Instruction is saved using JQuery to avoid postback. So how can I get the InstnID to save the InstnAttribute? I tried HiddenField to save InstnId but I am not able to receive the value there. Can we use viewdata directly in JQuery? Any help will be appreciable.
Upvotes: 0
Views: 6344
Reputation: 433
try this code
var fieldid = JSON.parse('@Html.Raw(Json.Encode(ViewData["fieldid"]))');
Upvotes: 1
Reputation: 11538
You need to pass the InstId in the PartialView and access it inside the PartialView. The best way to is to create a AttributeViewModel (which would contain the InstID) and pass it down the partial view.
jQuery can access it using inline code as :
var InstID = "<%= Model.InstID %>"
//make the jQuery postback call here using InstID
Upvotes: 1
Reputation: 75103
All my PartialViews like the one you are talking follow the same base:
they come up a little more complex, but that was the base I started with, and you will get everything work in no time!
Upvotes: 0
Reputation: 629
If your jQuery is defined in the markup (.aspx file) you can access the view data by assigning a variable to its value as follows.
var myValue = "<%= ViewData["MyValue"] %>";
alert(myValue);
The alert call is just to show that you have it. Obviously you can use it as you wish.
Upvotes: 3