I am building an online test webapp in asp.net mvc4.And I need to make the user to navigate between previous and next questions (only 1 question at a time). I have a layout which has a countdown timer in it. I have a question & options list as a model in the view and I need to navigate the list via previous and next buttons. The layout must remain same since it has a timer in it. When the user clicks on 'next', the current question should get back to list along with chosen answer (via radio buttons) and the next question from the list should be displayed. Similar function is done by previous button also. Can anyone please suggest a solution in Ajax,Json or Javascript ?
The view:StartTest
#model LoginTrial3.Models.QuestionPaper
#{
ViewBag.Title = "StartTest";
Layout = "~/Views/Shared/_LayoutTest.cshtml";
}
<h2>StartTest</h2>
Model : QuestionPaper
public class QuestionPaper
{
public List<object> RandomQuestionList = new List<object>();
//generate the maps to hold the answers
public Dictionary<int, string> AptiAnsList = new Dictionary<int, string>();
public Dictionary<int, string> TechAnsList = new Dictionary<int, string>();
//define a ctor
public QuestionPaper(List<object> QList)
{
foreach(var item in QList)
{
RandomQuestionList.Add(item);
}
}
}
each object in QList is of a AptiQuestionType :
public class AptiQuestion
{
public int Id { get; set; }
public string Question { get; set; }
public string OptionA { get; set; }
public string OptionB { get; set; }
public string OptionC { get; set; }
public string ExpectedAnswer { get; set; }
}
A solution without ajax could be to extend the model class and add ids of the previous and next items, then in the view render links using these ids.
Related
I have a controller that is sending a list of items to the API and then I'm getting back the item response, it means that I'm going to receive for example if the item was not found or not valid it is working fine!! but I would like to know how can I send the dtoresponse list to a view from my controller and what I need to add in the view?
DTO
public class ReceivedItemsRequest
{
public List<string> Items { get; set; }
}
public class ReceivedItemsResponse
{
public bool HasErrors { get; set; }
public List<ReceivedItemResponse> ItemResponses { get; set; } = new List<ReceivedItemResponse>() ;
}
public class ReceivedItemResponse
{
public string Barcode { get; set; }
public string ErrorMsg { get; set; }
}
}
Controller
[HttpPost]
public async Task<ActionResult> Received(List<string> itemsList)
{
try
{
var dtoRequest = new ReceivedItemsRequest();
var dtoResponse = new ReceivedItemsResponse();
dtoRequest.itemsList= itemsList;
dtoResponse = await API.Post<ReceivedItemsResponse, ReceivedItemsRequest>($"items/", dtoRequest);
return View(dtoResponse); ---> this part I'm not sure
how to send the list and what I need to add in the view or if I need to create a view model?
}
From your code, I discovered you are returning a single record not a List in the controller. So if your API return value is a single record you will send to the View like below:
Your Controller:
[HttpPost]
public async Task<ActionResult> Received(List<string> itemsList)
{
...
dtoResponse = await API.Post<ReceivedItemsResponse, ReceivedItemsRequest>($"items/", dtoRequest);
return View(dtoResponse);
}
Your View:
#model Application.ReceivedItemsResponse
#{
ViewBag.Title = "Received Items Response";
}
...HTML Code on View here...
if you are expecting a list as your dtoresponse then on your View you would have:
#model IEnumerable<Application.Models.ReceivedItemsResponse>
#{
ViewBag.Title = "Index Item Response";
}
...HTML Code Block here ....
Note that your controller does not change as long as you pass either a single record or list but your View does.
In an Umbraco MVC partial view I have declared a parameter of type List, based on a custom model.
List<MyModel> modelList = new List<MyModel>();
public class MyModel{
public int ArticleID { get; set; }
public string ArticleName { get; set; }
public string ArticleType { get; set; }
public bool isSelected { get; set; }
}
That list get populated with data from another model before being rendered in a row.
On a button click even I want to take that list and loop through it, any rows which have been selected have to be added to another list, the problem is I can't seem to access it in a function on the same view.
Here's what I have at the moment, I'm fine with populating the other list so I just put an alert in and put a breakpoint on it to see what is in modelList, but I get the error "AddToList is not defined" and I think it's to do with how I'm trying to access the list.
Can anyone tell me how I should be doing this please?
function AddToList() {
$.each(#modelList, function () {
alert("function hit");
});
}
First you need to convert your server side model into javascript array, after that you can perform each operation on it.
function AddToList()
{
var data = #Html.Raw(Json.Encode(Model));
$.each(data, function () {
alert("function hit");
});
}
I am trying to create a simple survey application and I am experiencing trouble accessing the survey's questions.
Survey Model
public class Survey
{
public Survey()
{
Questions = new List<Question>();
CompletedBy = new List<CompletedSurveys>();
}
public int SurveyID { get; set; }
public string Name { get; set; }
public List<Question> Questions { get; set; }
}
Question Model
public class Question
{
public int QuestionID { get; set; }
public int SurveyID { get; set; }
public string Title { get; set; }
}
And I auto generate input fields for the questions using the code below:
<input type="hidden" name="Questions['+ itemIndex +'].QuestionID"></input>
Which renders to:
<input type="hidden" name="Questions[0].QuestionID">
What am I doing wrong? It seems like I am inserting the questions into the list incorrectly. Since my model's questions are always null.
Any guidance would be greatly appreciated.
You don't need to JavaScript for generating hidden fields in this case. You can do it with a loop and #Html.Hidden like this:
for(int i = 0; i < Model.Questions.Count; i++)
{
#Html.Hidden(Model.Questions[i].QuestionID.ToString())
}
I am using Syncfusion Essential Studio Edition Version 14.2.0.28 . When i am working with gantt chart and want to indent subtask/subsession inside of parent task/session then it does not work.I followed bellow link
https:
//help.syncfusion.com/aspnetmvc/gantt/data-binding?cs-save-lang=1&cs-lang=csharp
Self-Referential Data Binding (Flat Data)
but till now does not work any one can suggest me? I shared 2 screen shot please follow
Real Data where data displayed without parent child indentation
Gantt Code snippet
To render the Gantt using Self Reference data source we need to consider the following things.
TaskId should be unique.
ParentId should be null for the parent Item.
For child items its parentId should be similar to its
relevant parent item’s task id.
We need to map the taskId field in “taskIdMapping” and parentId using “parentTaskIdMapping”.
Please find our online demo sample for your reference
Sample: http://mvc.syncfusion.com/demos/web/gantt/ganttselfreference
Regards,
Syncfusion Team
Syncfusion Team, Finally I solved the problem that i faced.When i am using your sample Code from gantt-> Data Binding -> Self Referencial Data Binding(Flat Data) Sample Code.
It seems to me where you need to do this correction.According to your example.
Syncfusion Sample Code Snippet:
public class Data
{
public string StartDate { get; set; }
public int Id { get; set; }
public int ParentId { get; set; }
public string Name { get; set; }
public int Duration { get; set; }
public int PercentDone { get; set; }
public List<Data> Children { get; set; }
public string Predescessor { get; set; }
}
The Change I made to make workable this sample is
public class Data
{
public string StartDate { get; set; }
public int Id { get; set; }
***public int? ParentId { get; set; }***
public string Name { get; set; }
public int Duration { get; set; }
public int PercentDone { get; set; }
public List<Data> Children { get; set; }
public string Predescessor { get; set; }
}
I am facing problem while using the object that i have sent from my controller to view using Json.
I am sending List of Objects to the View by using NewtonSoft.Json 7.x.x to serialize the List to Json. I am using the below code to serialize:
return JsonConvert.SerializeObject(DataToSend, Formatting.None, new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
I have 2 Entity Classes:
1) Form
2) FormFields
There is a 1-to-Many Relationship between these 2 entities.
public class Form
{
public long ID { get; set; }
public string Name { get; set; }
public virtual List<FormField> FormFields { get; set; }
}
public class FormField
{
public long FormID { get; set; }
public string FieldLabel { get; set; }
public string FieldType { get; set; }
public string FieldValue { get; set; }
public virtual Form form { get; set; }
}
I am trying to send the List of FormField to the view for rendering using Javascript. I am able to send it using the above serializaton Method.
But the problem is that, When i receive the Array of Objects in Javascript. It has Json Reference object IDs. I am not able to access those objects normally.
I am able to render the 1st FormField value in that array but i am not able to render rest of them. It is coming as Undefined.
I am attaching a screenshot of JSON Object Values which i am receiving on UI. You can see that there is an Object array. Each Object should have the Object of Type FormField and should have that field's value but there isn't.
Only the Object at index 0 is having the values and Rest of the Indexes have only Reference IDs.
Please help me resolve it.
Thanks
I don't know, if it is the best solution or not but I am posting so that may be it can save someone else's struggle to find the solution.
I Managed to do it using the .NET extension library plugin. Instead of using Newtonsoft.Json, I used System.Web.Extension DLL.
I put the attribute ScriptIgnore in my models as:
public class Form
{
public long ID { get; set; }
public string Name { get; set; }
[ScriptIgnore]
public virtual List<FormField> FormFields { get; set; }
}
public class FormField
{
public long FormID { get; set; }
public string FieldLabel { get; set; }
public string FieldType { get; set; }
public string FieldValue { get; set; }
[ScriptIgnore]
public virtual Form form { get; set; }
}
Then, I serialized it with the System.Web.Script.Serialization.JavaScriptSerializer as:
return new JavaScriptSerializer().Serialize(DataToSend);
It will serialize all the data but it will ignore the Properties which are having the attribute ScriptIgnore and it won't try to serialize them. For me it worked as required.
Just came to know that there is an Attribute JsonIgnore provided by NewtonSoft.Json that does the same job as ScriptIgnore. So, If you are using NewtonSoft Library then you can also use this.
It's good to know there are more ways to achieve the same result ;)