• 0

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /var/www/html/cnasolution/application/views/question.php
Line: 191
Function: _error_handler

File: /var/www/html/cnasolution/application/controllers/Questions.php
Line: 419
Function: view

File: /var/www/html/cnasolution/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Inserting template variable into block tag

Is it possible to create block names dynamically? I would like to do {% block {{obj.id}} %} but get an error saying this cannot be done I need to load custom content depending on obj into the block

Download script demo [LINK]
Download script demo [LINK 2] Download script demo [LINK 2]

The solution is to use the TempData property to store the desired Request components.

For instance:

public ActionResult Send() {     TempData["form"] = Request.Form;     return this.RedirectToAction(a => a.Form()); } 

Then in your "Form" action you can go:

public ActionResult Form() {     /* Declare viewData etc. */      if (TempData["form"] != null)     {         /* Cast TempData["form"] to          System.Collections.Specialized.NameValueCollection          and use it */     }      return View("Form", viewData); } 
see demo
  • 387
Reply Report

Keep in mind that TempData stores the form collection in session. If you don't like that behavior, you can implement the new ITempDataProvider interface and use some other mechanism for storing temp data. I wouldn't do that unless you know for a fact (via measurement and profiling) that the use of Session state is hurting you.

  • 136
Reply Report

There is another way which avoids tempdata. The pattern I like involves creating 1 action for both the original render and re-render of the invalid form. It goes something like this:

var form = new FooForm();  if (request.UrlReferrer == request.Url) {      // Fill form with previous request's data }  if (Request.IsPost()) {      if (!form.IsValid)      {          ViewData["ValidationErrors"] = ...      } else {          // update model          model.something = foo.something;          // handoff to post update action          return RedirectToAction("ModelUpdated", ... etc);      } }  // By default render 1 view until form is a valid post ViewData["Form"] = form; return View(); 

That's the pattern more or less. A little pseudoy. With this you can create 1 view to handle rendering the form, re-displaying the values (since the form will be filled with previous values), and showing error messages.

When the posting to this action, if its valid it transfers control over to another action.

I'm trying to make this pattern easy in the .net validation framework as we build out support for MVC.

  • 127
Reply Report