I'm new to MVC. How can I refresh part of my main view after submit action in one of my partial views. I'm trying to make adding comments. I didn't use ajax until now so any help is good. I want after I create new comment in PartialView "Create" that it appers in PartialView GetCommentsByTicket...I hope that this is enough explanation...
#model HelpDeskApp.Models.Ticket
#{
ViewBag.Title = "Details";
}
#*<h2>Details</h2>*#
<div class="panel panel-green">
<div class="panel-heading">
Ticket
</div>
<div class="panel-body">
<div>
#*<h4>ticket</h4>
<hr />*#
<dl class="dl-horizontal">
<dt>
#Html.DisplayNameFor(model => model.TicketId)
</dt>
<dd>
#Html.DisplayFor(model => model.TicketId)
</dd>
<dt>
#Html.DisplayNameFor(model => model.Subject)
</dt>
<dd>
#Html.DisplayFor(model => model.Subject)
</dd>
<dt>
#Html.DisplayNameFor(model => model.Description)
</dt>
<dd>
#Html.DisplayFor(model => model.Description)
</dd>
<dt>
#Html.DisplayNameFor(model => model.AddedDate)
</dt>
<dd>
#Html.DisplayFor(model => model.AddedDate)
</dd>
<dt>
#Html.DisplayNameFor(model => model.Category1.Name)
</dt>
<dd>
#Html.DisplayFor(model => model.Category1.Name)
</dd>
<dt>
#Html.DisplayNameFor(model => model.Priority1.Name)
</dt>
<dd>
#Html.DisplayFor(model => model.Priority1.Name)
</dd>
<dt>
#Html.DisplayNameFor(model => model.Status1.Name)
</dt>
<dd>
#Html.DisplayFor(model => model.Status1.Name)
</dd>
<dt>
#Html.DisplayNameFor(model => model.User.FirstName)
</dt>
<dd>
#Html.DisplayFor(model => model.User.FirstName)
</dd>
</dl>
</div>
<p>
#*#Html.ActionLink("Edit", "Edit", new { id = Model.TicketId }) |*#
#Html.ActionLink("Back to List", "Index")
</p>
<div id="postcomments">
<hr />
<h4>Komentari</h4>
#{ Html.RenderAction("CommentsByTicket", "Comments", new { ticketId = Model.TicketId });}
</div>
<div id="create comments">
#{ Html.RenderAction("Create", "Comments", new { Ticket = Model.TicketId });}
</div>
</div>
</div>
<div class="panel-footer">
Panel Footer
</div>
</div>
Related
CONTROLLER
In my controller I add product like this, cart is redirecting but not adding a product, how can this be fixed?
function tambah()
{
$data_produk= array('id' => $this->input->post('id'),
'nama_produk' => $this->input->post('nama_produk'),
'harga' => $this->input->post('harga'),
'gambar' => $this->input->post('gambar'),
//'seller' => $this->input->post('seller'),
'qty' => $this->input->post('qty')
);
//die(print_r($data_produk));
$this->cart->insert($data_produk);
$cart = $this->cart->contents();
if(!empty($cart)){
// print_r($cart);
}
redirect('product');
}
VIEW ;
<div class="col-md-8">
<div class="product_list">
<div class="row">
<?php foreach($shopall as $row){ ?>
<div class="col-lg-6 col-sm-6">
<div class="single_product_item">
<img src="assets/img/categori/product1.png" alt="" class="img-fluid">
<div class="card_area">
<form action="<?php echo base_url();?>cart/tambah" method="post" accept-charset="utf-8">
<div class="product_count_area" style="margin:-40PX -35PX 0PX 0PX;">
<p style="text-align:center;">Rp. <?php echo number_format($row['harga'],0,",",".");?></p>
<div class="product_count d-inline-block">
<span class="product_count_item inumber-decrement"> <i class="ti-minus"></i></span>
<input class="product_count_item input-number" name="qty" type="number" value="1" min="1" max="99">
<span class="product_count_item number-increment"> <i class="ti-plus"></i></span>
</div>
</div>
<p><?php echo $row['nama_produk'] ?></p>
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
<input type="hidden" name="nama_produk" value="<?php echo $row['nama_produk']; ?>" />
<input type="hidden" name="harga" value="<?php echo $row['harga']; ?>" />
<input type="hidden" name="gambar" value="<?php echo $row['gambar']; ?>" />
<div style="text-align:center; margin:-20px;" class="add_to_cart">
<!--<button type="submit" class="btn_3">add to cart</button>-->
<button type="submit" class="btn btn-sm btn-success"><i class="glyphicon glyphicon-shopping-cart"></i> Beli</button>
</div>
</form>
</div>
</div>
</div>
<?php } ?>
</div>
<div class="load_more_btn text-center">
Load More
</div>
</div>
</div>
In my last project I could use cart functionality with it, but in this project cart remains empty, can you point me where my code is wrong?
Codeigniter Cart Using English in it array keys, (unless you modified it somehow)
This should work - Notes: Add any extra data you may need to save under options, don't define your own array key. check here for more Codeingiter Cart
$data_produk = array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red')
);
instead of this
$data_produk= array('id' => $this->input->post('id'),
'nama_produk' => $this->input->post('nama_produk'),
'harga' => $this->input->post('harga'),
'gambar' => $this->input->post('gambar'),
//'seller' => $this->input->post('seller'),
'qty' => $this->input->post('qty')
);
Try to map that according to your language
I want to pass the Model.Game ID from Details page to another view page which is Cart.
The way I do is inserting the #Html.DisplayFor(model => model.Game.ID) in the app-route-id. Is that appropriate to do so?
Details.cshtml
#page "{id:int}"
#model GameSanctuary.Pages.Games.DetailsModel
#{
ViewData["Title"] = "Details";
}
<h2>Details</h2>
<div>
<h4>Game</h4>
<hr />
<dl class="dl-horizontal">
<dt>
#Html.DisplayNameFor(model => model.Game.G_Description)
</dt>
<dd>
#Html.DisplayFor(model => model.Game.G_Description)
</dd>
<dt>
#Html.DisplayNameFor(model => model.Game.G_Genre)
</dt>
<dd>
#Html.DisplayFor(model => model.Game.G_Genre)
</dd>
<dt>
#Html.DisplayNameFor(model => model.Game.G_Price)
</dt>
<dd>
#Html.DisplayFor(model => model.Game.G_Price)
</dd>
</dl>
</div>
<div>
<a asp-page="./Index">Back to List</a>
<a asp-page="./Cart" asp-page-handler="buynow" class="btn" asp-route-id="#Html.DisplayFor(model => model.Game.ID)">Add to Cart</a>
</div>
Cart.cshtml.cs
public async Task<IActionResult> OnGetBuyNow(int id)
{
cart = SessionHelper.GetObjectFromJson<List<Item>>(HttpContext.Session, "cart");
if (cart == null)
{
cart = new List<Item>();
cart.Add(new Item
{
Game = await _context.Game.FirstOrDefaultAsync(m => m.ID == id)
});
SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
}
return RedirectToPage("Cart");
}
The id value that I retrieved in OnGetBuyNow is 0 but the expected value should be 1 based on the model.Game.ID in Details.cshtml. Is that something missing in between?
no, it's not appropriate.
Html.DisplayFor() renders the display template which matches whatever type you use it on. That's not what you want. What you want is a simple number surely which can be injected directly like this:
asp-route-id="#Model.Game.ID"
I would start there and see if that gives you what you want
I tried to insert a breakpoint before each action and I was stuck at a point when ClassId value in HttpGet method takes the actual(correct)Id of the class which needs to be deleted but ClassId value in HttpPost method does not match with the ClassId value of the HttpGet method, infact value of ClassId in HttpPost returns null due to which I run into an exception error that object has null value. It would be really great if any one can acknowledge me that how that error can be removed. Thanks. Below is the code for the controller and the corresponding view respectively.
Controller Code
[HttpGet]
public IActionResult Delete(int ClassId)
{
return View(db.Class.Include(x => x.Student).First(c => c.ClassId == ClassId));
}
[ActionName("Delete")]
[HttpPost]
public IActionResult DeletePost(int ClassId)
{
var r = db.Class.First(a => a.ClassId == ClassId);
var student = r.StudentId.ToString();
db.Remove(r);
db.SaveChanges();
return RedirectToAction("Details", "Student", new { id = student });
}
View Code
#model DemoProject.Models.Class
#{
ViewData["Title"] = "Delete Class";
}
<h3>Are you sure you want to delete this?</h3>
<div>
<hr />
<dl class="dl-horizontal">
<dt>
#Html.DisplayNameFor(model => model.ClassName)
</dt>
<dd>
#Html.DisplayFor(model => model.ClassName)
</dd>
<dt>
#Html.DisplayNameFor(model => model.Title)
</dt>
<dd>
#Html.DisplayFor(model => model.Title)
</dd>
<dt>
#Html.DisplayNameFor(model => model.Text)
</dt>
<dd>
#Html.DisplayFor(model => model.Text)
</dd>
</dl>
<form asp-action="Delete">
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-danger" />
<div class="row">
<a asp-action="Details" asp-controller="Student" asp-route-id="#Model.Student.StudentId">Back</a>
</div>
</div>
</form>
</div>
You have two options:
As #stephen.vakil said, add a hidden field for the ClassId property to your view within the <form> element.
View with Hidden Field
#model ModelBinding.Models.Class
#{
ViewData["Title"] = "Delete Class";
}
<h3>Are you sure you want to delete this?</h3>
<div>
<hr/>
<dl class="dl-horizontal">
<dt>
#Html.DisplayNameFor(model => model.ClassName)
</dt>
<dd>
#Html.DisplayFor(model => model.ClassName)
</dd>
<dt>
#Html.DisplayNameFor(model => model.Title)
</dt>
<dd>
#Html.DisplayFor(model => model.Title)
</dd>
<dt>
#Html.DisplayNameFor(model => model.Text)
</dt>
<dd>
#Html.DisplayFor(model => model.Text)
</dd>
</dl>
<form asp-action="Delete">
<div class="form-actions no-color">
#Html.HiddenFor(model => model.ClassId)
<input type="submit" value="Delete" class="btn btn-danger"/>
<div class="row">
<a asp-action="Details" asp-controller="Student" asp-route-id="#Model.Student.StudentId">Back</a>
</div>
</div>
</form>
</div>
Add asp-route-id="#Model.ClassId" to your <form> tag and change the ClassId parameter name of your DeletePost method to id.
View with asp-route-id
#model ModelBinding.Models.Class
#{
ViewData["Title"] = "Delete Class";
}
<h3>Are you sure you want to delete this?</h3>
<div>
<hr/>
<dl class="dl-horizontal">
<dt>
#Html.DisplayNameFor(model => model.ClassName)
</dt>
<dd>
#Html.DisplayFor(model => model.ClassName)
</dd>
<dt>
#Html.DisplayNameFor(model => model.Title)
</dt>
<dd>
#Html.DisplayFor(model => model.Title)
</dd>
<dt>
#Html.DisplayNameFor(model => model.Text)
</dt>
<dd>
#Html.DisplayFor(model => model.Text)
</dd>
</dl>
<form asp-action="Delete" asp-route-id="#Model.ClassId">
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-danger"/>
<div class="row">
<a asp-action="Details" asp-controller="Student" asp-route-id="#Model.Student.StudentId">Back</a>
</div>
</div>
</form>
</div>
Controller with Renamed Action Parameter
[HttpGet]
public IActionResult Delete(int classId)
{
return View(db.Class.Include(x => x.Student).First(c => c.ClassId == classId));
}
[ActionName("Delete")]
[HttpPost]
public IActionResult DeletePost(int id)
{
var r = db.Class.First(a => a.ClassId == id);
var student = r.StudentId.ToString();
db.Remove(r);
db.SaveChanges();
return RedirectToAction("Details", "Student", new { id = student });
}
Unless you just can't use <hidden> fields for some reason, option 1 is the superior choice.
This is homework, an ASP.NET MVC and Web forms Web application using a repository (with data hard coded and no database) to rate doctors. The user should be able to edit and delete records, but when the application is run, and the "Delete" link is pressed for one of the records, the Delete page does not show the details of that record. Why not?
Here are the Delete methods:
public ActionResult Delete(string DoctorPicture)
{
repo.Remove(DoctorPicture);
return View("Delete");
}
[HttpPost]
public ActionResult Delete(string DoctorPicture, FormCollection collection)
{
try
{
// TODO: Add delete logic here
repo.Remove(DoctorPicture);
return View("Delete");
}
catch
{
return View();
}
}
From repo.Remove(DoctorPicture); it goes here in TestDoctorRepository, and doctor is null. Is it supposed to be?
public void Remove(string DoctorPicture)
{
var doctor = from d in doctors where d.DoctorPicture == DoctorPicture select d;
doctors.Remove(doctor.FirstOrDefault());
}
This is what my list of doctors looks like. Is my path for the images posing a problem?
public TestDoctorRepository()
{
doctors = new List<Doctor> {
new Doctor { DoctorPicture = "Images/0cropped.jpg", DoctorName = "Michael Shores", DoctorSpecialty = "Opthamology", times = 0, rating = 0, rated = true, avg=0, fave = true },
//more doctors
};
}
public Doctor GetDoctorByDoctorPicture(string DoctorPicture)
{
var selectedDoctor = from d in doctors
where d.DoctorPicture == DoctorPicture
select d;
return selectedDoctor.FirstOrDefault();
}
Delete View:
#model MidtermApplication.Models.Doctor
#{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<fieldset>
<legend>Doctor</legend>
<div class="display-label">
#Html.DisplayNameFor(model => model.DoctorPicture)
</div>
<div class="display-field">
#Html.DisplayFor(model => model.DoctorPicture)
</div>
<div class="display-label">
#Html.DisplayNameFor(model => model.DoctorName)
</div>
<div class="display-field">
#Html.DisplayFor(model => model.DoctorName)
</div>
<div class="display-label">
#Html.DisplayNameFor(model => model.DoctorSpecialty)
</div>
<div class="display-field">
#Html.DisplayFor(model => model.DoctorSpecialty)
</div>
<div class="display-label">
#Html.DisplayNameFor(model => model.rating)
</div>
<div class="display-field">
#Html.DisplayFor(model => model.rating)
</div>
<div class="display-label">
#Html.DisplayNameFor(model => model.times)
</div>
<div class="display-field">
#Html.DisplayFor(model => model.times)
</div>
<div class="display-label">
#Html.DisplayNameFor(model => model.fave)
</div>
<div class="display-field">
#Html.DisplayFor(model => model.fave)
</div>
<div class="display-label">
#Html.DisplayNameFor(model => model.rated)
</div>
<div class="display-field">
#Html.DisplayFor(model => model.rated)
</div>
</fieldset>
#using (Html.BeginForm()) {
<p>
<input type="submit" value="Delete" /> |
#Html.ActionLink("Back to List", "Index")
</p>
}
I have 2 validation forms on page.
<div id='LogOn' style="background-color: White;">
#using (Ajax.BeginForm("LogOnAjax", "Home", new AjaxOptions { UpdateTargetId = "LogOn", OnSuccess = "logInComplete" }))
{
//ITW2012Mobile.ViewModels.LogOnModel m = Model;
#Html.HiddenFor(model => model.IsLoggedIn)
#Html.HiddenFor(model => model.ReturnUrl)
<div>
#Html.ValidationSummary()
</div>
<div>
#Html.LabelFor(model => model.UserName)
#Html.EditorFor(model => model.UserName)
</div>
<div>
#Html.LabelFor(model => model.Password)
#Html.EditorFor(model => model.Password)
</div>
<div>
<input type="submit" value="Login" />
</div>
}
</div>
and
<fieldset>
#using (Html.BeginForm(Model.ActionMethod, Model.Controller))
{
#Html.ValidationSummary()
.....
<div class="display-label"> E-mail Address * </div>
<div class="display-field">
#Html.EditorFor(model => model.Username)
#Html.ValidationMessageFor(model => model.Username)
</div>
<div class="display-label"> Create Password * </div>
<div class="display-field">
#Html.PasswordFor(model => model.Password)
#Html.ValidationMessageFor(model => model.Password)
</div>
<p>
<input type="submit" value="Create" />
</p>
}
</fieldset>
How can I mark that ModelState.AddModelError is for first of form, because I see the same error in the both places of validation summary?
How can I mark that ModelState.AddModelError is for first of form
You can't.
What you can do is having ValidationMessageFor for each form properties instead of the ValidationSummary