0

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.

1
  • no, you can not use viewdata directly. try using cookies then, I know its a bad idea. Commented Nov 20, 2010 at 9:47

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

try this code

 var fieldid = JSON.parse('@Html.Raw(Json.Encode(ViewData["fieldid"]))');

Comments

0

All my PartialViews like the one you are talking follow the same base:

this cool article :)

they come up a little more complex, but that was the base I started with, and you will get everything work in no time!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.