Illegal characters in path in Html.RenderAction - asp.net

I am trying to call child action from view as below
#{
Html.RenderAction("Render", "ProgressBar", new { total = 10, completed = 3 });
};
and my controller code is as below
public class ProgressBarController : Controller
{
// GET: ProgressBar
[ChildActionOnly]
public ActionResult Render(int total, int completed)
{
ViewBag.Total = total;
ViewBag.Completed = completed;
ViewBag.Percent = ((completed * 100) / total).ToString("0.#####");
return PartialView();
}
}
and partial view for Render
<div class="section-progress-wrapper">
<div class="section-progress-info clearfix">
<div class="section-progress-label float-l">
<span class="section-progress-output">#ViewBag.Completed</span>
<span class="section-progress-desc">of</span>
<span class="section-progress-total">#ViewBag.Total</span> modules completed
</div>
<div class="section-progress-icon float-r"></div>
</div>
<div class="section-progress-box">
<div id="progressBar" class="section-progress-range" style="width:#string.Format("{0}%",ViewBag.Percent)"></div>
</div>
</div>
but for some reason I am getting exception saying
System.ArgumentException: Illegal characters in path.
I am not able to figure out what I am doing wrong. Can someone please help?

Related

data-reveal loading partial view MVC

I have the following code on my cshtml page:
<div class="large reveal" id="draftProductModal" data-reveal>
<button class="close-button" data-close aria-label="Close modal" type="button">
<span aria-hidden="true"><i class="fa fa-times-circle-o"></i></span>
</button>
</div>
<div class="input-group">
<input type="text" class="input-group-field" id="draftSearchProducts" name="draftSearchProducts" placeholder="SearchProducts" />
<div class="input-group-button">
<!--Add product to order lines button-->
<a id="draftAddProduct" class="hollow button secondary" data-open="draftProductModal"><i class="fa fa-plus"></i> Search Products</a>
</div>
</div>
I need to take the value in the draftSearchProducts text field, pass it to a controller that returns a partialview:
public ActionResult SearchResults(string keywords, int queryLimit = 20, int offset = 0)
{
try
{
//connect to db, grab data return results
searchResults.ProductDetails = products.ToList();
return PartialView("_SearchResults", searchResults);
}
catch (Exception ex)
{
throw ex;
}
}
I need to send in the keyword to the controller, return the partial view and load it into the draftProductModal div and display the div. How is this done? I am new to front end development.
Tried this code:
var url = '/ProductsController/SearchResults';
$('#draftAddProduct').click(function () {
var keyWord = $('draftSearchProducts').val();
$('#draftProductModal').load(url, { keywords: keyWord });
});
And all I get is a 404 error, so it appears I am not hitting my controller. I think I am getting close, I just need to find out how to access the controller ActionResult. Still accepting help.

Form submit error, Failed to convert value of type 'java.lang.String' to required type error in browser, In spring MVC

So, I'm trying to create comments on a post using spring mvc, spring boot, spring data, jpa, and thymeleaf, and so far I can get to the specific page I want, using the controller and pathvariables, and I can load up the page just how I want, but when I go to submit the comment I get the error
There was an unexpected error (type=Bad Request, status=400).
Failed to convert value of type 'java.lang.String' to required type 'com.example.domain.Comment'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type java.lang.Long for value 'comment 1'; nested exception is java.lang.NumberFormatException: For input string: "comment1"
This error is only in my browser, nothing comes up in the console in my IDE. Also I can access the page just fine, so there I don't think there's an issue in my get method in my controller, but I'm not really sure where the problem is, so I'll show you guys some of my code.
Here's my controller.
private PostRepository postRepo;
#RequestMapping(value="viewCourse/post/{postId}", method=RequestMethod.GET)
public String postViewGet (#PathVariable Long postId, ModelMap model)
{
Post post = postRepo.findOne(postId);
model.put("post", post);
Comment comment = new Comment();
model.put("comment", comment);
return "post";
}
#RequestMapping(value="viewCourse/post/{postId}", method=RequestMethod.POST)
public String postViewPost (#ModelAttribute Comment comment, #PathVariable Long postId, ModelMap model)
{
Post post = postRepo.findOne(postId);
comment.setPost(post);
post.getComments().add(comment);
postRepo.save(post);
return "redirect:/viewCourse/{postId}";
}
#Autowired
public void setPostRepo(PostRepository postRepo) {
this.postRepo = postRepo;
}
Here's my thymeleaf html page
<div class="PostContent">
<h2 th:text = "${post.title}"></h2>
<p th:text = "${post.content}"></p>
</div>
<br/>
<div class="CommentPost">
<form th:action="${post.id}" method="post" th:object="${comment}" id="comment">
<div class="form-group">
<textarea rows="2" th:field="${comment.comment}" class="form-control" placeholder="comment" id="comment"></textarea>
</div>
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
<input type="submit" value="Comment" class="btn btn-success"/>
</form>
</div>
<br/>
<div class="Comments">
<div th:each = "comment : ${comments}" th:object="${comment}">
<span th:text="${comment.comment}"></span>
</div>
<div th:if = "${#lists.isEmpty(comments)}">
There are no comments to display
</div>
</div>
</div>
Also on this page the message comes up "There are no comments to display", just like I tell it to in the code, but it still says "There are no comments to display" even if I manually insert a comment into the database.
Here's my comment object, although I'm pretty sure that's fine.
#Entity
public class Comment {
public Long id;
public String comment;
public Post post;
public User user;
#Id
#GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
#ManyToOne
public Post getPost() {
return post;
}
public void setPost(Post post) {
this.post = post;
}
#ManyToOne
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
And my postRepo, although this should be fine, just thought I'd include it
public interface PostRepository extends JpaRepository <Post, Long>{
}
If anyone can see my issue, and let me know, that would be awesome, thanks.
When you use th:object don't have to reference to object, you access directly atributes of object. Try with this code:
<div class="PostContent">
<h2 th:text = "${post.title}"></h2>
<p th:text = "${post.content}"></p>
</div>
<br/>
<div class="CommentPost">
<form th:action="${post.id}" method="post" th:object="${comment}" id="comment">
<div class="form-group">
<textarea rows="2" th:field="*{comment}" class="form-control" placeholder="comment" id="comment"></textarea>
</div>
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
<input type="submit" value="Comment" class="btn btn-success"/>
</form>
</div>
<br/>
I don't see in the controller where you put the comments in the model. I suppose that comments there are inside the post so modify the refereces of comments to post.comments
<div th:each = "comment : ${post.comments}" th:object="${comment}">
<span th:text="*{comment}"></span>
</div>
<div th:if = "${#lists.isEmpty(post.comments)}">
There are no comments to display
</div>
</div>
</div>
The problem is that the name of Class - Comment - and the field - comment - are the same, regarding to insensitive way, causing problem due to Java Reflection use to read the field and its class.
The solution was to rename the field, like "comment" to "commentary", and to avoid to change again in database, if there is some, just put the annotation #Column(name="comment") above the field.
Perhaps, reference from main template on thymeleaf template in question look like:
th:href="#{/post/{${post.getId()}}",
but it should look like:
th:href="#{/post/{postId}(postId=${post.getId()})}"
In my occasion, it helped me

How to access implicit objects in my templates

I want to know what is the current url of my template. I have read here that the "request" is an implicit object present in all templates which returns the url of my template.
So I have tried this:
Controllers:
public class Application extends Controller {
public static Result index() {
return redirect("/home");
}
public static Result home() {
return ok(homePage.render());
}
public static Result aboutUs() {
return ok(aboutUs.render());
}
}
HTML:
leftbar.scala.html file:
<aside id="left-panel">
<nav>
<ul class="animated fadeInLeft">
<li class="#if(request.uri.contains("/aboutus")){active}">(some code here)</li>
</ul>
</nav>
</aside>
homePage.scala.html file:
#scripts = { (some scripts here) }
#views.html.main("Beta Project", scripts) {
#views.html.leftbar()
<div id="main" role="main">
<div id="content">
<span><i class="fa fa-bell"></i>SOMETHING</span>
</div>
</div>
}
Where I import the leftbar.scala.html file I get this error:
not found: value request
What should I do to solve this error? Thanks in advance
A bit more code from your template would be useful, but you can probably fix it by adding
(implicit request: Request[AnyContent])
at the end of the first line on your template.

Asp.net mvc Html.Action render at wrong position

the code is
<div class="container" >
#Html.Action("aaa","bbb");
</div>
the action output string "123",
What I want is
<div class="container" >
123
</div>
but actually the result is
123
<div class="container" >
</div>
the action result place in wrong position ,why and how resolve ,Thanks in advance
action code below
public ActionResult Test(string custEmpId)
{
model.code = "123";
return PartialView(model);
}

Strongly Typed Model with MvcMailer

i am facing troubles passing strongly typed model to MvcMailer view. I am using asp.net mvc3 and installed MvcMailer3 using nuget.
There is no error messages and message is sent successfully but the data fields are not populated. I am doing everything also tried using ViewBag fields but the same problem- that is message sent successfully but fields are not populated...
please help...i am stuck for two days!
here is my controller and view code....
//Controller code
public ActionResult Index()
{
string invoicenumber = "BC-00000002";
IEnumerable<Quantum.Models.usp_MasterPrintInvoice_Result> mpi = db.usp_MasterPrintInvoice(invoicenumber);
IEnumerable<PrintDetailObject> printdetailobj = from anmpi in mpi select new PrintDetailObject { Head = anmpi.Head, ContributorName = anmpi.Name, RegistrationNumber = anmpi.RegistrationNumber, InvoiceNumber = anmpi.InvoiceNumber, InvoiceDate = anmpi.Date, Amount = anmpi.Amount, PaymentMonth = anmpi.Month, ReceivedBy = anmpi.Operator };
ViewData.Model = printdetailobj.FirstOrDefault();
IUserMailer mailer = new UserMailer();
mailer.Welcome().Send();
return View();
}
View Code
#model Quantum.Models.PrintDetailObject
#using Mvc.Mailer
<h2>Invoice</h2>
<fieldset>
<div class="print-details-page">
<legend>#Html.DisplayFor(model => model.Head)</legend>
<div class="display-label">InvoiceNumber</div>
<div class="display-field">
#Html.DisplayFor(model => model.InvoiceNumber)
</div>
<div class="clear"></div>
<div class="display-label">Date</div>
<div class="display-field">
#Html.DisplayFor(model => model.InvoiceDate)
</div>
<div class="clear"></div>
<div class="display-label">Recieved From:</div>
<div class="display-field">
#Html.DisplayFor(model => model.ContributorName)
</div>
<div class="clear"></div>
<div class="display-label">Registration Number:</div>
<div class="display-field">
#Html.DisplayFor(model => model.RegistrationNumber)
</div>
<div class="clear"></div>
<div class="display-label">Amount:</div>
<div class="display-field">
#Html.DisplayFor(model => model.Amount)
</div>
<div class="clear"></div>
<div class="display-label">Amount in Text:</div>
<div class="display-field">
#Html.DisplayFor(model => model.AmountText)
</div>
<div class="clear"></div>
<div class="display-label">Month:</div>
<div class="display-field">
#Html.DisplayFor(model => model.PaymentMonth)
</div>
<div class="clear"></div>
<div class="display-label">Recieved By:</div>
<div class="display-field">
#Html.DisplayFor(model => model.ReceivedBy)
</div>
<div class="clear"></div>
<br />
<p>Received with many thanks.</p>
</div>
</fieldset>
I am only get the following text as the email body:
Invoice
InvoiceNumber
Date
Recieved From:
Registration Number:
Amount:
Amount in Text:
Month:
Recieved By:
Received with many thanks.
hi i have just got the insight to answer my own question.
actually when i used ViewData.Model inside of the "SendEmail" Controller the model is available to its own view namely the "Index" view instead of the razor view used with the "UserMailer" Controller which is named "Welcome" in my case.
To pass the Model to "UserMailer" action we need to do the following modification:
//IUserMailer code
...
public interface IUserMailer
{
MvcMailMessage Welcome(PrintDetailObject myModel);
}
//UserMailer code
public virtual MvcMailMessage Welcome(PrintDetailObject myModel)
{
//ViewBag.Data = someObject;
ViewData.Model = myModel;
return Populate(x =>
{
x.Subject = "Welcome";
x.ViewName = "Welcome";
x.To.Add("cmmwahid#hotmail.com");
});
}
//and finally controller code
public ActionResult Index()
{
string invoicenumber = "BC-00000002";
IEnumerable<Quantum.Models.usp_MasterPrintInvoice_Result> mpi = db.usp_MasterPrintInvoice(invoicenumber);
IEnumerable<PrintDetailObject> printdetailobj = from anmpi in mpi select new PrintDetailObject { Head = anmpi.Head, ContributorName = anmpi.Name, RegistrationNumber = anmpi.RegistrationNumber, InvoiceNumber = anmpi.InvoiceNumber, InvoiceDate = anmpi.Date, Amount = anmpi.Amount, PaymentMonth = anmpi.Month, ReceivedBy = anmpi.Operator };
IUserMailer mailer = new UserMailer();
mailer.Welcome(printdetailobj.FirstOrDefault()).Send();
return View();
}
here is the final output as a email body:
Invoice
Generous Contribution
InvoiceNumber
BC-00000002
Date
15/02/2014
Recieved From:
Some Donor
Registration Number:
M104/6
Amount:
$23.00
Amount in Text:
Twenty Three Dollars Only
Month:
May/2014
Recieved By:
someuser
Received with many thanks.

Resources