Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Wednesday, December 2, 2015

MVC: Display string as HTML from database to view

Following is the way to display html in view:

For aspx:
<%= System.Web.HttpUtility.HtmlDecode(@Model.Html) %>

For Razor:

@Html.Raw(Server.HtmlDecode(@Model.Html))

Friday, February 20, 2015

Server.MapPath in MVC - HostingEnvironment.MapPath

Server.MapPath is throwing error in MVC? use HostingEnvironment.MapPath instead.

HostingEnvironment.MapPath needs to include to use System.Web.Hosting.

Following is the sample code:

var file = System.IO.File.AppendText(System.Web.Hosting.HostingEnvironment.MapPath("~/logs/error.txt"));

Tuesday, February 10, 2015

@Html.ActionLink("Application name", "Index", "Home", null, new { @class = "navbar-brand" }) object routevalues decoded

@Html.ActionLink("Application name", "Index", "Home", null, new { @class = "navbar-brand" })
4th Parameter in @Html.ActionLink is routeValues and of object type. This can be used to set query string and manage resulting anchor tag.

Following line will render an anchor tag with 3 query string parameters:
@Html.ActionLink("Application name", "Index", "Home", new { page = 1,name="my name",gogs="ola" }, new { @class = "navbar-brand" })

Result: http://localhost:34446/Home/Index/1?name=my%20name&gogs=ola



Similarly following code will return an anchor tag with id in url(not in query string parameter):

@Html.ActionLink("Application name", "Index", "Home", new { id = 1 }, new { @class = "navbar-brand" })

Result: http://localhost:34446/Home/Index/1

MVC: Cannot create/shadow copy 'System.Web.Mvc' when that file already exists

This is a temporary build error. Following steps would help you resolve this error:


  1. Wait for some time(30 sec to 1 min).
  2. Right click Solution and clean solution.
  3. Rebuild Solution.
  4. Open some other url in browser bar.
  5. Try reloading same url again.
Hope this resolves your error. However if this still does not work for you, then go to temp location (asp.net temp files in program files) and delete all the project related temporary files(dlls). And rebuild solution. You might need to close visual studio as few files could be in use.


This should resolve the error. If you face such errors very frequently, I would recommend you to clear your machine. Delete all the temp files. empty recycle bin, delete unnecessary files from system drive(c drive), clean desktop.

And you still face such errors(and other like visual studio is busy performing some internal operation), in worst case you might need to fix your windows.(if you are unable to do this, get your machine formatted if possible).

Monday, January 19, 2015

XMLHttpRequest cannot load no 'access-control-allow-origin' header is present on the requested resource. Origin is therefore not allowed access

This error comes when a web service is request by browser (using jQuery or JavaScript). Put following httpProtocol node in system.webServer in Web.config file to resolve this issue:

  <system.webServer>
      .
      .
      .
      .
      <httpProtocol>
            <customHeaders>
                  <add name="Access-Control-Allow-Origin" value="*"/>
                  <add name="Access-Control-Allow-Headers" value="Content-Type"/>
                  <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS"/>
            </customHeaders>
      </httpProtocol>

  </system.webServer>

Monday, December 29, 2014

MVC Razor: Working with Hidden Fields. Set and Get Hidden Fields.

MVC Razor: Working with Hidden Fields. Set and Get Hidden Fields.
Following code will render a hidden field in MVC Razor view:

<div class="row">
    @using (Html.BeginForm("UpdateModelInfo", "Model", FormMethod.Post))
    {
        @Html.HiddenFor(p => p.ModelItem.Id)
        <div class="form-group col-xs-12">
            @Html.TextAreaFor(p => p.ModelItem.ModelInfo, new { @class = "css-class", rows = 25 })
        </div>
        <div class="col-xs-12">
            <button type="submit" onclick="javascript:GetVal();" class="btn-class">Get Val</button>
            <button type="submit" class="btn-class">Submit</button>
        </div>
    }
</div>
We can also update this hidden field like any other element using jQuery or JavaScript:

<script type="text/javascript">
    function GetVal() {
        alert($('#hdn').val());
    }
</script>
Following is the way to get hidden field value in controller:

[HttpPost]
public ActionResult UpdateMoreInfo(FormCollection collection)
{
    Core.models.Model modl = new Core.models.Families();
    var modlId = collection.GetValue("ModelItem.Id").AttemptedValue;
    var moreInfo = collection.GetValue("ModelItem.ModelInfo").AttemptedValue;
    //perform some other logic here
    Db.SaveChanges();

    return RedirectToAction("Edit", "Model", new { @id = modlId });
}