MVC Session Not Showing in PartialView - asp.net-mvc

I am doing a post back to a controller and setting Sessions. After they are set, it shows up in the first tab, but when I click next tab that loads another partialview, I do not see sessions set.
order.cshtml with Tabs...
<div id="tabs">
<ul class="nav nav-tabs">
<li>General</li>
<li>Item</li>
<li>Total</li>
</ul>
<div id="tabs-1">
#{Html.RenderPartial("_Partial_General_Tab", Model.GeneralTab);}
</div>
<div id="tabs-2">
#{Html.RenderPartial("_Partial_Item_Tab", Model.ItemTab);}
</div>
<div id="tabs-3">
Content for Tab 3 goes here.<br />
</div>
</div>
Tab 1
#model Mvc5.Models.ORDERMetadata
#{
var ordernumber = (int)Session["Order_Number"];
var princid_session = Session["PrincId"];
var custid_session = Session["CustId"];
}
OrderNumber: #ordernumber <br />
PrinicId: #princid_session <br />
CustID: #custid_session <br />
#using (Ajax.BeginForm("Edit", "Order",
new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "target"
}))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
Enter model data here....
<button id="editorder" type="submit" class="btn btn-default">Save</button>
}
Tab 2
#model Mvc5.Models.ORDER_DETAILSMetadata
#{
var ordernumber = (int)Session["Order_Number"];
var princid_session = Session["PrincId"];
var custid_session = Session["CustId"];
}
OrderNumber: #ordernumber <br />
PrinicId: #princid_session <br />
CustID: #custid_session <br />
#using (Ajax.BeginForm("Items", "Order",
new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "target"
}))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
Enter model data here....
<input type="submit" value="Add" class="btn btn-default" />
}
Edit Controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(ORDERMetadata model)
{
try
{
// update order
....
db.Entry(order).State = EntityState.Modified;
db.SaveChanges();
Session.Clear();
Session["Order_Number"] = model.Order_Number;
Session["PrincId"] = model.Princ_ID;
Session["CustId"] = model.Cust_ID;
return PartialView("_Partial_General_Tab", model);
}
catch (DbEntityValidationException ex)
{
return RedirectToAction("Index", "Error");
}
}

Related

Can't trigger my controllers action with a date time in the route values

Here is my form. If I remove the classdate parameter it works fine, but I can't trigger the controller if I have a date in the routevalues
using (Html.BeginForm("Results", "Home", new { classDate = DateTime.Now }, FormMethod.Get, new { #class = "navbar-form navbar-left", role = "search" }))
{
<div class="form-group">
<input type="text" class="form-control" placeholder="Search" id="navbarSearchQueryWithin" name="location">
</div>
<button type="submit" class="btn btn-default">Submit</button>
}
Here is my controller
[HttpGet]
public ActionResult Results(string navbarSearchQueryWithin, DateTime classDate)
{
var viewModel = new YogaSpaceListViewModel
{
SearchQuery = navbarSearchQueryWithin
};
return View("Search", viewModel);
}

Ajax.BeginForm return PartialView code does not update

I am doing a login logout functionality using Ajax partial. For this I have a View which checks if the user is logged in or not and shows login or logout form accordingly.
On submit it does a ajax request and logs in or out the user. after doing so in controller I return same partial view.
So expected behavior is on return partial view must again check for login status and refresh the view accordingly, but instead same form is loaded.
Partial View:
#model Models.LoginModel
#if (Member.MemberIsLoggedOn())
{
using (Ajax.BeginForm("LoginForm", "Account", null, new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "login-form-update",
},new {
#class="loginform form"
}))
{
<div class="col-md-12 padding-zero">
<div class="row flt-right">
Hello #Context.User.Identity.Name, <input type="submit" name="logout" class="btn btn-default" value="Log Out" />
</div>
</div>
}
}
else
{
using (Ajax.BeginForm("LoginForm", "Account", null, new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "login-form-update",
}, new {
#class = "loginform form"
}))
{
<div class="col-md-12 padding-zero">
<div class="row flt-right">
<div class="form-group col-md-5">
#Html.TextBoxFor(x => Model.Username, new { #class = "form-control", #placeholder = "Username" })
</div>
<div class="form-group col-md-5">
#Html.TextBoxFor(x => Model.Password, new { #class = "form-control", #placeholder = "Password", #type = "Password" })
</div>
<div class="form-group col-md-2 flt-right">
<input type="submit" name="login" class="btn btn-default" value="Go" />
</div>
</div>
</div>
}
}
Controller:
public class AccountController : Controller
{
[HttpPost]
public ActionResult LoginForm(LoginModel model)
{
if (!ModelState.IsValid)
{
//Do nothing
}
// Login
if (Membership.ValidateUser(model.Username, model.Password))
{
FormsAuthentication.SetAuthCookie(model.Username, false);
return PartialView("Header/LoginForm", new Models.LoginModel());
}
else
{
ModelState.AddModelError("Username", "Username is not valid");
//do nothing
}
}
public ActionResult Logout()
{
FormsAuthentication.SignOut();
Session.Clear();
return PartialView("Header/LoginForm", new Models.LoginModel());
}
}
Now my problem is login/logout happens properly, but changes do not reflect unless page is refreshed, which is i want to avoid by Ajax.BeginForm(),
update
If i click two times the view changes, but this is not a good user experience.
I think this might be due to cache problem. You need to use output cache attribute to disable the cache for that action method .
You can use something like this.
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
Also clear your ModelState before the return .
ModelState.Clear();
return PartialView(model);

File upload not working as expected

I am trying to upload a file from my application and it is not working as expected. I am not getting any file into the action method.
I have my viewmodel as below
public class CallQueryViewModel
{
[Display(Name = "Attachment")]
public HttpPostedFileBase UploadFile { get; set; }
}
And my Razor form is as below
#{
var ajaxOptions = new AjaxOptions
{
HttpMethod = "POST",
OnBegin = "onCallAddBegin",
OnSuccess = "OnCallCreateSuccess",
OnFailure = "OnCallCreateFailure"
};
}
#using (Ajax.BeginForm("AddCall", "CallHandling", ajaxOptions, new { #id = "CallAddForm", enctype = "multipart/form-data" }))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<div class="row">
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.UploadFile, new { #class = "col-md-2" })
#Html.TextBoxFor(model => model.UploadFile, new { #class = "col-md-10", type="file" })
</div>
</div>
</div>
<div id="Submit">
<input type="submit" value="Save" class="btn btn-success" />
</div>
}
And my controller action method is as below.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddCall(CallQueryViewModel model)
{
if ((model.UploadFile != null) && (model.UploadFile.ContentLength > 0))
{
// Upload file.
}
}
With the above code, when i am trying to upload the file, I am not receiving any file into the controller action method. The UploadFile is always coming as null.
Could someone suggest what I am missing ?

"Controller, Action, Model" not all code paths return a value

I am really confused by this error "not all code paths return a value" on my action PostResponse. I have stared at my model, controller and view for hours and I think I have all paths covered. Of course the project won't build, so I can't debug further.
My action
// POST: /Questions/ViewQuestion/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult PostResponse([Bind(Include = "UserId,QuestionID,Answer,Source,Status,DateStamp")] Response response)
{
if (ModelState.IsValid)
{
db.Responses.Add(response);
db.SaveChanges();
}
}
My view
#model Template.Models.Question
#using Microsoft.AspNet.Identity
#{
ViewBag.Title = "View question";
var qtype = Model.QuestionTypeId;
ViewBag.Number = Model.Id - 7;
}
#using (Html.BeginForm("Question", "ViewQuestion", FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
<div>
<h4>Question ##ViewBag.Number</h4>
<hr />
<h1> #Model.Question1</h1>
</div>
<div class="form-group">
#switch (qtype)
{
case 1:
// Textbox
#Html.TextArea("Answer", new { #class = "form-control", rows = "4", col = "5" });
break;
case 2:
// Dropdown
<select class="form-control" id="Answer">
#foreach (var item in Model.QuestionOptions.OrderBy(o => o.QuestionOptionRanking))
{
<option value="#item.QuestionOption1">#item.QuestionOption1</option>
}
</select>
break;
case 3:
// Checkbox
<div class="checkbox">
#foreach (var item in Model.QuestionOptions.OrderBy(o => o.QuestionOptionRanking))
{
<input type="checkbox" name="Answer" value="#item.QuestionOption1" /> #item.QuestionOption1 <br />
}
</div>
break;
case 4:
// Radio buttons
foreach (var item in Model.QuestionOptions.OrderBy(o => o.QuestionOptionRanking))
{
<div class="radio">
<label>
<input type="radio" name="Answer" value="#item.QuestionOption1" />
#item.QuestionOption1
</label>
</div>
}
break;
}
</div>
#using Template.Models.Response
#Html.HiddenFor(r => r.Responses, new { UserId = User.Identity.GetUserId(), Source = "Web", Status = "New", DateStamp = System.DateTime.Now })
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Answer" />
</div>
</div>
<br />
<hr />
<p>
#Html.ActionLink("Previous", "ViewQuestion", new { id = Model.Id - 1 }) |
#Html.ActionLink("Next", "ViewQuestion", new { id = Model.Id + 1 })
</p>
The page displays perfectly, but I can't test the post action as I cannot build with the current error.
Worked it out; but it was almost from scratch, as I created a new ViewModel and used that to populate the responses.
[HttpPost]
public ActionResult ViewQuestion([Bind(Include = "QuestionId, Answer, UserId")] ResponseViewModel responseViewModel)
{
Response re = new Models.Response();
re.Answer = responseViewModel.Answer;
re.UserId = responseViewModel.UserId;
re.QuestionId = responseViewModel.QuestionId;
re.DateStamp = System.DateTime.Now;
db.Responses.Add(re);
db.SaveChanges();
return RedirectToAction("ViewQuestion");
}
Thanks for your input as your comments got the old head working again. Thanks!
Your PostResponse action, or method specifies an ActionResult as a return type, but does not actually return anything. You can resolve this by changing it from ActionResult to void
try
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult PostResponse([Bind(Include = "UserId,QuestionID,Answer,Source,Status,DateStamp")] Response response)
{
if (ModelState.IsValid)
{
db.Responses.Add(response);
db.SaveChanges();
}
else{
return View("Error");
}
}

How to display a model state error in case I am returning a partial view

I have the following action method for creating new network info:-
public ActionResult CreateVMNetwork(int vmid)
{
VMAssignIps vmips = new VMAssignIps()
{
TechnologyIP = new TechnologyIP() { TechnologyID = vmid},
IsTMSIPUnique = true,
IsTMSMACUnique = true
};
return PartialView("_CreateVMNetwork",vmips);
}
[HttpPost]
public ActionResult CreateVMNetwork(VMAssignIps vmip)
{
if (ModelState.IsValid)
{
try
{
repository.InsertOrUpdateVMIPs(vmip.TechnologyIP,User.Identity.Name);
repository.Save();
return PartialView("_networkrow",vmip);
}
catch (Exception ex)
{
ModelState.AddModelError(string.Empty, "Error occurred: " + ex.InnerException.Message);
}
}
return PartialView("_CreateVMNetwork", vmip);
}
And I have the following _CreateVMNetwork view:-
#model TMS.ViewModels.VMAssignIps
#using (Ajax.BeginForm("CreateVMNetwork", "VirtualMachine", new AjaxOptions
{
InsertionMode = InsertionMode.InsertAfter,
UpdateTargetId = "networktable",
LoadingElementId = "loadingimag",
HttpMethod= "POST"
}))
{
#Html.ValidationSummary(true)
#Html.HiddenFor(model=>model.TechnologyIP.TechnologyID)
#Html.Partial("_CreateOrEditVMNetwork", Model)
<input type="submit" value="Save" class="btn btn-primary"/>
}
and _CreateOrEditVMNetwork view:-
#model TMS.ViewModels.VMAssignIps
<div>
<span class="f">IP Address</span>
#Html.EditorFor(model => model.TechnologyIP.IPAddress)
#Html.ValidationMessageFor(model => model.TechnologyIP.IPAddress)
<input type="CheckBox" name="IsTMSIPUnique" value="true" #(Html.Raw(Model.IsTMSMACUnique ? "checked=\"checked\"" : "")) /> |
<span class="f"> MAC Address</span>
#Html.EditorFor(model => model.TechnologyIP.MACAddress)
#Html.ValidationMessageFor(model => model.TechnologyIP.MACAddress)
<input type="CheckBox" name="IsTMSMACUnique" value="true" #(Html.Raw(Model.IsTMSMACUnique ? "checked=\"checked\"" : "")) />
</div>
The problem I am facing is that in case there is a model state error when adding a new entity, a partial view will be displayed with the model state error as follow:-
So my question is , if there is a way to display the model state error with the partial view , without updating the table row “insert after” as I am doing currently?
Thanks
Given the age i'm guessing you have already found a solution to this,
But here is an example using InsertionMode.Replace, maybe it can help someone else.
Snipped from view
#using (Ajax.BeginForm("AddPerson", "Home", new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "UpdateSection" }))
{
<div id="UpdateSection">
#Html.Partial("PersonModel", Model.Person)
</div>
<input type="submit" value="add" />
}
Snipped from the controller
if (!ModelState.IsValid)
{
return PartialView("AddPerson", Person);
}
just make sure the "jquery.unobtrusive-ajax.min.js" script is included (i'm not sure it is by default)

Resources