Controller function called 2 times at the same time MVC 4 - asp.net-mvc

my controller is called twice at the same time, i don't know what's wrong with the code
Controller:
List<Item> lstItems = new List<Item>();
public ActionResult _SaveItem(ItemDetails model)
{
Item it = new Item();
it.ItemName = model.itemName;
it.ItemURL = model.itemUrl;
it.Notes = model.Notes;
it.Price = model.unitprice;
it.Quantity = model.quantity;
it.Size = model.Size;
it.Weight = model.weight;
it.CategoryID = model.CategoryID;
it.Color = model.color;
lstItems.Add(it);
return PartialView(it);
}
View:
#model CSP1225.Models.ItemDetails
#using (#Ajax.BeginForm("_SaveItem", "Order", new AjaxOptions { InsertionMode = InsertionMode.InsertAfter, UpdateTargetId="Items" }))
{
<div id="Items">
</div>
<div class="form-horizontal" style="padding-left:20px;" >
#Html.EditorForModel()
<input type="Submit" id="addItem" value="Add Item" name="addItem" />
</div>
}
any help please.. ??

Actually i included jquery.unobtrusive-ajax Twice , that's why the code called twice at the same time :)

Related

Retrieve partial view data

I have menu and using this I'm rendering partial view in my index page
#Ajax.ActionLink("week I", "firstWeekWinners", new
{
}, new AjaxOptions()
{
HttpMethod = "GET",
UpdateTargetId = "weeWinnersDIv",
InsertionMode = InsertionMode.Replace
}, new { #class = "weekDays wactive" })
and there's partial view controller:
public PartialViewResult firstWeekWinners()
{
ViewBag.laptopWinners = db.firstWeekLaptopWinner().ToList();
ViewBag.tabWinners = db.firstWeekGalaxyWinner().ToList();
ViewBag.ipodWinners = db.firstWeekIpodWinner().ToList();
ViewBag.headPhonesWinners = db.firstWeekHeadPhonesWinner().ToList();
ViewBag.kingstonWinners = db.firstWeekkingstonUsbWinner().ToList();
return PartialView();
}
and partial view:
#if (ViewBag.laptopWinners != null)
{
<div class="winnersPrizeOverflow">
<div class="winnersPrizePic"></div>
<div class="winnersAllprizeLeft"><p>12</p></div>
</div>
foreach (var q in ViewBag.laptopWinners)
{
<span>#q.firstName</span>
<br />
<span>#q.lastName</span>
<br />
<span>#q.fbId</span>
<br />
<span>#q.sumOfBids</span>
<br />
}
}
If I'll click to this ajax.actionlink it renders this view but without this if I'll put #Html.Partial("firstWeekWinners") it doesn't goes in controller to retrieve my data.
How can I improve that?
To call your firstWeekWinners method use #Html.Action(), not #Html.Partial()
#Html.Action("firstWeekWinners")
Refer to the documentation for the various overloads, and these question/answers explaining the differences and usage.

MVC Session Not Showing in PartialView

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");
}
}

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